Length of string in python


#1

how to find the length of the string without spaces in python?


#2

Count the number of number of spaces and subtract from the length


#3

is there any command like len for this?what if there is very long string how we will count then?


#4

Why would the length affect how you do the counting? You’re going to have to look through the string and count the spaces, there’s no way around that. The length is already stored though, so no looping is required for that. Space is by no means a special character, so the number of spaces is not stored.

There does exist a string method for counting occurrences of a substring, yes.
https://docs.python.org/2/library/string.html#string.count
It’s also trivial to write a loop for that purpose.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.