写一个函数,输入一行字符,将此字符串中最长的单词输出
时间: 2023-04-30 08:03:21 浏览: 95
```python
def find_longest_word(sentence):
words = sentence.split(" ")
longest_word = ""
for word in words:
if len(word) > len(longest_word):
longest_word = word
return longest_word
```
输入一行字符串,使用split(" ")将其分割成单词列表,然后遍历列表中的每个单词,如果当前单词的长度比目前最长单词长,就将当前单词设置为最长单词。最后返回最长单词。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)