Use this:
s = "python is pretty fun to use"
[len(x) for x in s.split()]
example output:
>>> [len(x) for x in s.split()]
[6, 2, 6, 3, 2, 3]
What's going on in the background?
s.split()
breaks on the white space in the string and returns each word in the sentence in a list:
>>> s.split()
['python', 'is', 'pretty', 'fun', 'to', 'use']
Then we take the len()
of each of those words to get the word's length. After that, we take each length and append it to a list so that it can be conveniently returned as the result.
That all happens in this list comprehension:
[len(x) for x in s.split()]
Still a little confused? This is conceptually the same thing just broken down more explicitly:
results = []
for x in s.split():
word_length = len(x)
results.append(word_length)
print results
If you'd like them printed out separately, like in your question, use:
for x in [len(x) for x in s.split()]:
print x