Strings in Python are immutable sequences of Unicode code points. Given a string, we need to find its length. Examples:
Input : 'abc' Output : 3 Input : 'hello world !' Output : 13 Input : ' h e l l o ' Output :14
Methods#1:
- Using the built-in function len. The built-in function len returns the number of items in a container.
Python3
# Python code to demonstrate string length # using len str = "Lazyroar" print ( len ( str )) |
5
Method#2:
- 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.
Python3
# Python code to demonstrate string length # using for loop # Returns length of string def findLen( str ): counter = 0 for i in str : counter + = 1 return counter str = "Lazyroar" print (findLen( str )) |
5
Method#3:
- 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.
Python3
# Python code to demonstrate string length # using while loop. # Returns length of string def findLen( str ): counter = 0 while str [counter:]: counter + = 1 return counter str = "Lazyroar" print (findLen( str )) |
5
Method#4:
- 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.
Python3
# Python code to demonstrate string length # using join and count # Returns length of string def findLen( str ): if not str : return 0 else : some_random_str = 'py' return ((some_random_str).join( str )).count(some_random_str) + 1 str = "Lazyroar" print (findLen( str )) |
5
Method:5:
- Using reduce method. Reduce method is used to iterate over the string and return a result of collection element provided to the reduce function. We will iterate over the string character by character and count 1 to result each time.
Python3
# Python code to demonstrate string length # using reduce import functools def findLen(string): return functools. reduce ( lambda x,y: x + 1 , string, 0 ) # Driver Code string = 'Lazyroar' print (findLen(string)) |
Output:
5
Method:6:
- Using sum() and list comprehension function. We use list comprehension for iterating over the string and sum function to sum total characters in string
Python3
# Python code to demonstrate string length # using sum def findLen(string): return sum ( 1 for i in string); # Driver Code string = 'Lazyroar' print (findLen(string)) |
Output:
5
Method 7: Using enumerate function
Python3
# python code to find the length of # string using enumerate function string = "gee@1ks" s = 0 for i, a in enumerate (string): s + = 1 print (s) |
7