Find length of a string in python (4 ways)
Strings in Python are immutable sequences of Unicode code points. Given a string, we need to find its length.
Examples:
Input : 'hello world !' Output : 12 Input : ' h e l l o ' Output :14
Methods:
- Using the built-in function len. The built-in function len returns the number of items in a container.
# Python code to demonstrate string length# using lenstr="geeks"print(len(str))chevron_rightfilter_noneOutput:5
- Using for loop and in operator. A string can be iterated over, directly in a for loop. Maintaining a count of the number of iterations will result in the length of the string.
# Python code to demonstrate string length# using for loop# Returns length of stringdeffindLen(str):counter=0foriinstr:counter+=1returncounterstr="geeks"print(findLen(str))chevron_rightfilter_noneOutput:5
- Using while loop and Slicing. We slice a string making it shorter by 1 at each iteration will eventually result in an empty string. This is when while loop stops. Maintaining a count of the number of iterations will result in the length of the string.
# Python code to demonstrate string length# using while loop.# Returns length of stringdeffindLen(str):counter=0whilestr[counter:]:counter+=1returncounterstr="geeks"print(findLen(str))chevron_rightfilter_noneOutput:5
- Using string methods join and count. The join method of strings takes in an iterable and returns a string which is the concatenation of the strings in the iterable. The separator between the elements is the original string on which the method is called. Using join and counting the joined string in the original string will also result in the length of the string.
# Python code to demonstrate string length# using join and count# Returns length of stringdeffindLen(str):ifnotstr:return0else:some_random_str='py'return((some_random_str).join(str)).count(some_random_str)+1str="geeks"print(findLen(str))chevron_rightfilter_noneOutput:5
Recommended Posts:
- Python | Ways to find length of list
- Python | Ways to find all permutation of a string
- Python | Ways to find nth occurrence of substring in a string
- Python | Ways to split a string in different ways
- Reverse string in Python (5 different ways)
- Python | Ways to check if given string contains only letter
- Ways to remove i'th character from string in Python
- Python | Ways to check string contain all same characters
- Python | Ways to concatenate boolean to string
- Python | Ways to sort letters of string alphabetically
- Python | Ways to convert list of ASCII value to string
- Python | Ways to count number of substring in string
- Python | Ways to remove n characters from start of given string
- Python | Ways to remove numeric digits from given string
- Python | Ways to convert string to json object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



