用replace编程输入一个字符串 hi!好?ok,行。,把里面的标点符号全换成空格
时间: 2023-05-19 07:01:56 浏览: 66
Sure, here's the code:
```python
s = "hi!好?ok,行。"
s = s.replace('!', ' ').replace('?', ' ').replace(',', ' ').replace('。', ' ')
print(s)
```
The output will be:
```
hi 好 ok 行
```
相关问题
编程实现:任意输入一个字符串,把里面的标点符号全换成空格。提示: in 或 replace ()方法
Python代码实现:
```python
string = input("请输入一个字符串:") # 输入一个字符串
punctuation = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' # 定义标点符号
for char in string:
if char in punctuation:
string = string.replace(char, " ") # 替换标点符号为空格
print("替换后的字符串为:", string)
```
输出示例:
```
请输入一个字符串:Hello, World! This is a string.
替换后的字符串为: Hello World This is a string
```
任意输入一个字符串,把里面的标点符号全换成空格
好的,以下是代码:
```python
import string
def replace_punctuation_with_space(s):
return s.translate(str.maketrans(string.punctuation, ' ' * len(string.punctuation)))
input_str = input("请输入一个字符串:")
output_str = replace_punctuation_with_space(input_str)
print(output_str)
```
这个函数会将输入字符串中的所有标点符号都替换成空格,并返回替换后的字符串。