写一个函数,输入一行字符,将此字符中最长的单词输出
时间: 2023-04-15 11:00:59 浏览: 59
可以使用以下函数实现:
```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
```
使用方法:
```python
sentence = "I love Python programming language"
longest_word = find_longest_word(sentence)
print(longest_word) # 输出 "programming"
```
阅读全文