同时打印字符串的单词及其长度

Print the words of the string and their length along the same time

希望大家身体健康....

一段时间以来,我一直在尝试获得我编写的其中一个代码的结果....但无法获得所需的结果...如果你能帮助我,我会非常高兴...☺☺

我有一个字符串,我需要同时打印字符串的单词和它们的长度,中间有一个冒号.....这是我的 code 这是我需要的结果:

[This:4 is: 2 pretty: 6]

漂亮

但是当我尝试打印结果的第一行时出现错误....

试试这个:

sentence = 'This is pretty'
result = [f'{w}:{len(w)}' for w in sentence.split()]

这是结果:

>>> result
['This:4', 'is:2', 'pretty:6']
word_count=[]
for w in words:
    c = w + ":" + str(len(w)) 
    word_count.append(c)

print(word_count)

输出:

['This:4', 'is:2', 'pretty:6']
sent = "Hello Word!"

print([f"{word}:{len(word)}" for word in sent.split()])

您正在寻找这样的东西吗?

sent = "This is pretty"
words = sent.split(" ")
print([word + ":"+str(len(word)) for word in words])
#print(list(word,":",len(word)))

length = [len(word) for word in words]
maximum=max(length)
text_index=length.index(maximum)
longest_word=words[text_index]
print(longest_word )

输出:

['This:4', 'is:2', 'pretty:6']
pretty