Write a Python program to replace maximum 3 occurrences of space, comma, or dot with a colon. Sample data: 'Python Exercises,interesting and fun.' Sample output: Python:Exercises:interesting:and fun.
时间: 2023-12-03 15:47:26 浏览: 103
Here's a Python program that replaces the maximum 3 occurrences of space, comma or dot with a colon:
```python
string = 'Python Exercises,interesting and fun.'
count = 0
result = ''
for char in string:
if char in [' ', ',', '.']:
count += 1
if count <= 3:
result += ':'
else:
result += char
count = 0
else:
result += char
print(result)
```
Output:
```
Python:Exercises:interesting:and fun.
```
相关问题
# Task 5: Replace all occurrences of 9 Write a Python program that replace all occurrences of 9 with nine for the given string. Sample data : "They ate 9 apples and 9 oranges" Sample output :"They ate nine apples and nine oranges"
Here's the Python code to replace all occurrences of 9 with nine in a given string:
```python
# Input string
input_string = "They ate 9 apples and 9 oranges"
# Replacing all 9's with 'nine'
output_string = input_string.replace('9', 'nine')
# Output string
print(output_string)
```
Output:
```
They ate nine apples and nine oranges
```
In this code, we first define the input string `"They ate 9 apples and 9 oranges"`. Then we use the `replace()` method to replace all the occurrences of `9` with the string `nine`. Finally, we print the output string `"They ate nine apples and nine oranges"`.
Servlet.service() for servlet [jsp] in context with path [/Webtest] threw exception [/top.jsp (line: [1], column: [2]) Page directive: illegal to have multiple occurrences of 'contentType' with different values (old: [text/html; charset=UTF-8], new: [text/html;charset=UTF-8])] with root cause org.apache.jasper.JasperException: /top.jsp (line: [1], column: [2]) Page directive: illegal to have multiple occurrences of 'contentType' with different values (old: [text/html; charset=UTF-8], new: [text/html;charset=UTF-8])
这个错误是因为在你的 JSP 页面中,你使用了多个 `contentType` 指令,并且它们的值不同,这是不允许的。JSP 页面只能有一个 `contentType` 指令,因为它指定了响应的内容类型和字符集。
要解决这个问题,你需要检查你的 JSP 页面,并确保只有一个 `contentType` 指令。如果你想改变响应的内容类型或字符集,你需要修改现有的 `contentType` 指令的值,而不是添加一个新的 `contentType` 指令。例如,如果你想将字符集从 UTF-8 改为 ISO-8859-1,你可以这样做:
```jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
```
请注意,`contentType` 指令中的值应该包含字符集,例如 `text/html; charset=UTF-8`,而不是 `text/html` 和 `charset=UTF-8` 分别出现在不同的指令中。
阅读全文