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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python code to demonstrate string length 
    # using len
      
    str = "geeks"
    print(len(str))

    chevron_right

    
    

    Output:

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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 = "geeks"
    print(findLen(str))

    chevron_right

    
    

    Output:

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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 = "geeks"
    print(findLen(str))

    chevron_right

    
    

    Output:

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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 = "geeks"
    print(findLen(str))

    chevron_right

    
    

    Output:

    5
    


My Personal Notes arrow_drop_up

A full stack web developer Python enthusiast and instructor

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.