Length of string in python


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


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


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


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.
It’s also trivial to write a loop for that purpose.


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