How to get the length of words in a sentence?

python

Solution

Try this, using `map()` for applying `len()` over each word in the sentence, understanding that `split()` creates a list with each word in the sentence:

s = "python is pretty fun to use"
map(len, s.split())       # assuming Python 2.x
list(map(len, s.split())) # assuming Python 3.x

Or alternatively, you can use a list comprehension for the same effect:

[len(x) for x in s.split()]

In both cases the result is a list with the length of each word in the sentence:

[6, 2, 6, 3, 2, 3]

Problem

I am trying to get the length of each word in a sentence. I know you can use the "len" function, I just don't know how to get the length of each word. Instead of this ``` >>> s = "python is pretty fun to use" >>> len(s) 27 >>> ``` I'd like to get this ``` 6, 2, 6, 3, 2, 3 ``` which is the actual length of every word.

Original source