python2. 已知字符串: s="Python,,PyThin,,,PYthen,pYThon,pyThon,,PatHon,pytion,pYTHON,,paTHiN,pYtian" (1) 删除所有的逗号 (2)所有单词都改成首字母大写其他字母小写的形式
时间: 2024-10-08 18:21:51 浏览: 27
QtCreator启动报错 “由于找不到python310.dll”
首先,让我们处理删除所有逗号的问题:
```python
s = "Python,,PyThin,,,PYthen,pYThon,pyThon,,PatHon,pytion,pYTHON,,paTHiN,pYtian"
# 删除所有逗号
s_no_commas = s.replace(",", "")
```
现在,我们对字符串进行单词分割,并将每个单词转换为首字母大写、其余字母小写的格式:
```python
words = s_no_commas.split()
capitalized_words = [word.capitalize() for word in words]
result = " ".join(capitalized_words)
```
完整的代码如下:
```python
s = "Python,,PyThin,,,PYthen,pYThon,pyThon,,PatHon,pytion,pYTHON,,paTHiN,pYtian"
s_no_commas = s.replace(",", "")
words = s_no_commas.split()
# 首字母大写并连接成新字符串
capitalized_words = [word.capitalize() for word in words]
result = " ".join(capitalized_words)
print(result)
```
阅读全文