str = "他问, 你 知 道cba和nba是什么单词的缩写么 ? " print('原文本内容:{}'.format(str)) print('1.删除空格') print('2.英文标点替换') print('3.字母大写') print('4.退出') newstr=str while True: option = input("请输入功能选项:\n") if option=='1': # 删除所有空格 #newstr = ?????? print('删除所有空格后:',newstr) elif option =='2': # 替换英文标点 #?????????? print('替换英文标点后:',newstr) elif option =='3': # 所有字母大写用upper()实现 #?????????????? print('所有字母大写后:',newstr) elif option == '4': break
时间: 2023-06-09 13:06:51 浏览: 61
1. 删除空格:用于去除字符串中的空格(包括中英文空格)。
代码如下:
newstr = str.replace(' ','')
print('处理后的内容:{}'.format(newstr))
解释:用字符串方法replace()将空格替换为空字符串,从而去除空格。
输出:处理后的内容:他问,你知道cba和nba是什么单词的缩写么?
如果要去除所有空格,包括中英文空格、制表符等,可以使用正则表达式:
import re
newstr = re.sub(r'\s+','',str)
print('处理后的内容:{}'.format(newstr))
解释:用re模块中的sub()方法,指定正则表达式'\s+',表示匹配一个或多个空白字符,然后用空字符串替换。
输出:处理后的内容:他问,你知道cba和nba是什么单词的缩写么?
如果要保留一部分空格,比如问号和逗号后面的空格,可以采取以下方式:
newstr = str.replace(' ',',').replace('?',' ?').replace('!',' !')
print('处理后的内容:{}'.format(newstr))
解释:将空格替换成逗号,再将问号和叹号前面的空格替换回来。
输出:处理后的内容:他问,你知道cba和nba是什么单词的缩写么 ?
相关问题
str = "他问, 你 知 道cba和nba是什么单词的缩写么 ? " print('原文本内容:{}'.format(str)) print('1.删除空格') print('2.英文标点替换') print('3.字母大写') print('4.退出') while True: option = input("请输入功能选项:\n") if option=='1': # 删除所有空格 #newstr = ?????? print('删除所有空格后:',newstr) elif option =='2': # 替换英文标点 #?????????? print('替换英文标点后:',newstr) elif option =='3': # 所有字母大写用upper()实现 #?????????????? print('所有字母大写后:',newstr) elif option == '4': break
str = "他问, 你 知 道cba和nba是什么单词的缩写么 ? "
print('原文本内容:{}'.format(str))
print('1.删除空格')
print('2.英文标点替换')
print('3.字母大写')
print('4.退出')
while True:
option = input("请输入功能选项:\n")
if option == '1':
str = str.replace(' ', '')
print(str)
elif option == '2':
str = str.replace(',', '').replace('?', '').replace(' ', '')
print(str)
elif option == '3':
str = str.upper()
print(str)
elif option == '4':
break
else:
print("请输入正确的选项!")
tr = "他问, 你 知 道cba和nba是什么单词的缩写么 ? " print('原文本内容:{}'.format(str)) print('1.删除空格') print('2.英文标点替换') print('3.字母大写') print('4.退出') while True: option = input("请输入功能选项:\n") if option=='1': # 删除所有空格 #newstr = ?????? print('删除所有空格后:',newstr) elif option =='2': # 替换英文标点 #?????????? print('替换英文标点后:',newstr) elif option =='3': # 所有字母大写用upper()实现 #?????????????? print('所有字母大写后:',newstr) elif option == '4': break
答案:
tr = "他问,你知道CBA和NBA是什么单词的缩写吗?"
print('原文本内容:{}'.format(tr))
print('1.删除空格')
print('2.英文标点替换')
print('3.字母大写')
print('4.退出')
while True:
option = input("请输入功能选项:\n")
if option == '1':
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)