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

Recommended Answers

All 2 Replies

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

[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

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.