0

i need help understanding how to fix my code (But please no explicit answers, I really need to understand.)
I need to create a program (I'm using IDLE) that will count the number of characters in a given sentence WITHOUT counting spaces.

So far I have:

S = raw_input("Enter Sentence: ")
print "The number of characters in the sentence is %d" % (len(S))

But this does not subtract the spaces. I tried using S.split, but i'm not using it correctly, for instance:

S = raw_input("Enter Sentence: ")
S.strip()
print "The number of characters in the sentence is %d" % (len(S))

thanks for any help/advice

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by sfar_furqan
0

Strip only remove spaces from eaither side

You can loop through the string yourself and count each character that isn't a space (recommended if this is homework)(you can loop through strings just like you would loop through a list),

or

you can use S.count(" ") and subtract that from len(S) (NOT recommended for homework).

0
[code=python]
import string
S = raw_input("Enter Sentence: ")
countSpace=string.count(S," ")
countTotal= len(S)
totalCharacter=countTotal-countSpace
print totalCharacter

you can also try this

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.