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

Question:

Hoping you all to be fit and fine….

I have been trying since a while to get the result of one of the codes that I have written….but unable to get the desired result…Would be more than happy if you could help me out…☺☺

I have got a string and I need to print the words of the string and their length along the same time with a colon in between…..This is my
code
and This is the result I am in need:

[This:4 is: 2 pretty: 6]

pretty

But an error is popping up while I am trying to print the first line of the result….

Asked By: Suha Akrami

||

Answers:

Try this:

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

This is the result:

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

print(word_count)

Output:

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

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

You’re looking for something like this?

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 )

Output:

['This:4', 'is:2', 'pretty:6']
pretty
Answered By: Python learner