I am a newbie to python. I am just working on a program to count number of letters in each words of a sentence. That includes punctuation too. If the sentence is "This is it!" my program should give "4 2 3". First I thought I can convert this string into word list and then count the letters of the word but I am totally lost on that. Later used string.split() to count the number of words. But still couldn't figure out how to calcuate the number of letters in a word that I splitted.

I would appreciate if someone teach me the concept on how to acheive it. I searched alot on net but couldnt actually find one.

Recommended Answers

All 8 Replies

Look into using list comprehesion or basic for loop with append. There is also map function that would fit your case but now the prefered way are the more versatile list comprehensions.

Also len(word) gives the number of letters in a word.

A little help or mayby to much.

>>> s = "This is it!".split()
>>> s
['This', 'is', 'it!']
>>> len(s[0])
4
>>> [len(i) for i in s]
[4, 2, 3]

Try to figure out what happens in code over.
And as a practise [len(i) for i in s] write this line as a basic for loop with append as pyTony suggest.
This line called list comprehesion and are very common to use in python,so it good to look into it even if you have just start learing python.

def lens():
   print([len(i) for i in (raw_input("Insert sentence: ")).split(' ')])
lens()

Here it's printed the list formed by numbers from the statement

len(i)

where i is a word (string) from the

raw_input("Insert sentence: ")

you have inserted, which is splitted:

.split(' ')

Notice that the

.split()

function has as the delimiter the empty space

' '

, so that it splits the sentence after each SPACE you have inserted. So, in other words, this function will print the lenght of each word resulted in the split of the given sentence.

Output:

>>> print([len(i) for i in (raw_input("Insert sentence: ")).split(' ')])
Insert sentence: Hello World.
[5, 6]

There is no need to use split(' ') Lucaci,just use split().
split() always splits on all whitespace if left unspecified.

Thank you all for your help. Now I learned the string function and the use of list comprehension. It was amazing that I can get the output in just few lines of code.

Once again thank you all.

snippsat I put it there to evidentiate the face that, when dealing with these kind of situations, it's better to use a delimiter. In his case, the delimiter is the whitespace, which can be accomplished by just calling the .split() function.
In other cases, if the requirement is different, one should know about these things.
I do agree that the use of .split() is more suitable in this case, as of typing a sentence as "This is a sentence " will yield the output [4,2,1,8,0] with the whitespace character as the delimiter, as with .split() will just ignore the last whitespace character.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.