+ 1
Counting number of letters in string without spaces count
How you can count the number of letters in string without counting spaces? I tried with for loop but it counting spaces as well. Anyone help please
3 Antworten
+ 5
there is a very simple method to do this: (if you assume that everything except space is a letter)
▪︎as a sample: text = 'How are you?'
▪︎get length (=number of total characters) of the string by using len(text)
▪︎ get number of spaces by using text.count(' ')
▪︎ subtract number of spaces from total length
you can do this with one line of code
! happy coding !
+ 1
Try using isalpha()
Google how to use it
0
You can try with any letters in string, just need code for total counts of letter in string.
For instance, check this one by me. But it counts spaces as well and gives output 12 including 9 letters with 3 spaces
#code
text = 'How are you?'
count = 0
for i in text:
count+=1
print(count)