def find_longest_word(string):
d = []
a = string.split()
for x in a:
b = (x.count("") - 1)
d.append(b)
f = max(d)
print (x, f)
find_longest_word("hello my name is k")
該程序將打印每個單詞的長度最長旁邊每個,但我只希望它打印最長的單詞。請幫忙。如何讓程序只顯示最長的單詞而不是每個字符串中的單詞?
使用'len'獲得Python中的字符串的長度,如果你不希望它打印了一堆的話,就把'print'外循環。 – jpmc26