How do you find the length of a string in python?

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python len() function returns the length of the string. 

    Python len() Syntax

    Syntax: len(string) 

    Return: It returns an integer which is the length of the string. 

    Python len() Example

    len() methods with string in Python.

    Python3

    string = "Geeksforgeeks"

    print(len(string))

    Output:

    13

    Example 1: Len() function with tuples and string

    Here we are counting length of the tuples and list.

    Python

    tup = (1,2,3)

    print(len(tup))

    l = [1,2,3,4]

    print(len(l))

    Output: 

    3
    4

    Example 2: Python len() TypeError

    Python3

    How do you find the length of a string in python?

    Output:

    TypeError: object of type 'bool' has no len()

    Example 3: Python len() with dictionaries and sets

    Python3

    dic = {'a':1, 'b': 2}

    print(len(dic))

    s = { 1, 2, 3, 4}

    print(len(s))

    Output:

    2
    4

    Example 4: Python len() with custom objects

    Python3

    class Public:

        def __init__(self, number):

            self.number = number

        def __len__(self):

            return self.number

    obj = Public(12)

    print(len(obj))

    Output:

    12

    Time complexity : 

    len() function  has time complexity of O(1). in average and amortized case


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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

    str = "geeks"

    print(len(str))

    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

    def findLen(str):

        counter = 0   

        for i in str:

            counter += 1

        return counter

    str = "geeks"

    print(findLen(str))

    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

    def findLen(str):

        counter = 0

        while str[counter:]:

            counter += 1

        return counter

    str = "geeks"

    print(findLen(str))

    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

    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))

    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

    import functools

    def findLen(string):

        return functools.reduce(lambda x,y: x+1, string, 0)

    string = 'geeks'

    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

    def findLen(string):

        return sum( 1 for i in string);

    string = 'geeks'

    print(findLen(string))

    Output:

    5

    What does Len () do in Python?

    The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.

    How do you find the length of a string string?

    Java String length() method example.
    public class LengthExample{.
    public static void main(String args[]){.
    String s1="javatpoint";.
    String s2="python";.
    System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string..

    How do I print a specific length of a string in Python?

    Summary:.
    len() is a built-in function in python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc..
    Value: the given value you want the length of..
    Return value a return an integer value i.e. the length of the given string, or array, or list, or collections..