Search code examples
pythoncompiler-errorsstring-length

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


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....


Solution

  • sent = "Hello Word!"
    
    print([f"{word}:{len(word)}" for word in sent.split()])