将一英文语句中所有单词的首字母转换成大写
时间: 2024-05-07 22:21:15 浏览: 119
转换首字母大写
4星 · 用户满意度95%
可以使用 Python 中的 `.title()` 方法来实现:
```python
sentence = "this is a sentence."
new_sentence = sentence.title()
print(new_sentence) # 输出 "This Is A Sentence."
```
或者使用 `.split()` 方法将句子拆分成单词,再对每个单词的首字母进行大写转换,最后再组合成一个新的句子:
```python
sentence = "this is a sentence."
words = sentence.split()
new_sentence = ""
for word in words:
new_sentence += word.capitalize() + " "
print(new_sentence.strip()) # 输出 "This Is A Sentence."
```
阅读全文