Python中有一个字符串'i am a student. you are a teacher.'编程将字符串中的每条语句的第一个单词首字母改写为大写,并输出字符数最多的那条语句
时间: 2024-06-11 16:07:19 浏览: 91
Python大作业之jmu-java&python-统计字符个数.txt
代码如下:
```python
s = 'i am a student. you are a teacher.'
sentences = s.split('. ') # 以'. '为分隔符将字符串分割成句子列表
max_len = 0 # 最长的句子长度
max_sentence = '' # 最长的句子
for sentence in sentences:
words = sentence.split(' ') # 以空格为分隔符将句子分割成单词列表
words[0] = words[0].capitalize() # 将第一个单词首字母大写
new_sentence = ' '.join(words) # 将单词列表合并成句子
if len(new_sentence) > max_len: # 更新最长的句子
max_len = len(new_sentence)
max_sentence = new_sentence
print(max_sentence) # 输出最长的句子
```
输出结果:
```
You are a teacher.
```
阅读全文