Built with Tutify.io

    AIStudyLabs

    Python String length with Examples

    Python String length with Examples

    In this article, we will discuss several ways to return the length of the given string in Python. A string is a set of characters similar to C, C++, and other programming languages.

    <a id="h-1-quick-examples-of-string-length"></a>

    1. Quick Examples of String length

    
    # Consider the string
    string1 = 'sparkby examples - Python tutorials'
    print("String: ",string1)
    
    # Length of string using len() function
    print("String Length using len(): ",len(string1))
    
    # Length of string using for loop
    it = 0   
    for i in string1:
        it = it + 1
    print("String Length using for loop: ",it)
    
    # Length of string using while loop
    it = 0   
    while (string1[it:]):
            it = it + 1 
    print("String Length using while loop: ",it)
    
    # Length of string using sum()
    print("String Length using sum(): ",sum(1 for i in string1))
    

    <a id="h-2-string-length-using-len-function"></a>

    2. String length using len() function

    len() function is used to return the total number of characters including spaces present in the given string.

    <a id="h-2-1-syntax-of-len-function"></a>

    2.1 Syntax of len() function

    The following is the syntax of len() function.

    
    # Here string1 is the input string.
    len(string1)
    

    <a id="h-2-2-parameters-of-len"></a>

    2.2 Parameters of len()

    len() function takes only one parameter i.e string name.

    <a id="h-2-3-return-of-len"></a>

    2.3 Return of len()

    len() will return the total number of characters present in the string which is an integer value.

    <a id="h-2-4-string-len-example"></a>

    2.4 String len() Example

    In this example, we will create a string and return the length of this string.

    
    # Consider the string
    string1 = 'sparkby examples - Python tutorials'
    print("String: ",string1)
    
    # Length of string
    print("String Length: ",len(string1))
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    

    We can see that there are total 35 characters present in the string including spaces.

    <a id="h-3-string-length-using-for-loop"></a>

    3. String length using for loop

    In this scenario, we will use a for loop to iterate each character in a string and store it in a variable. Inside for loop, we will increment this variable. Finally we will display this variable that hold the total number of characters present in the string.

    <a id="h-3-1-syntax-structure"></a>

    3.1 Syntax Structure

    
    # Here i is the iterator
    # it variable stores the total number of characters.
    
    it = 0   
    for i in string1:
        it = it + 1
    

    <a id="h-3-2-string-length-with-for-loop-example"></a>

    3.2 String length with for loop example

    In this example, we will create a string and return the length of this string using for loop.

    
    # Consider the string
    string1 = 'sparkby examples - Python tutorials'
    print("String: ",string1)
    
    it = 0   
    for i in string1:
        it = it + 1
    
    # Length of string
    print("String Length: ",it)
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    

    We can see that there are total 35 characters present in the string including spaces.

    We can also implement using user-defined function.

    
    # Consider the string
    string1 = "sparkby examples - Python tutorials"
    print("String: ",string1)
    
    def length_fun(string1):
      it = 0   
      for i in string1:
        it = it + 1
      return it
    
    # Length of string
    print("String Length: ",length_fun(string1))
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    

    <a id="h-4-string-length-using-while-loop"></a>

    4. String length using while loop

    In this scenario, we will use a while loop to iterate each character in a string and store it in a variable. In while loop, we will use slice operator to iterate the string from first character to last. Inside the while loop we will increment the variable that stores the character count.

    <a id="h-4-1-syntax-structure"></a>

    4.1 Syntax Structure

    
    it = 0   
    while (string1[it:]):
            it = it + 1
    

    <a id="h-4-2-string-length-with-while-loop-example"></a>

    4.2 String length with while loop example

    In this example, we will create a string and return the length of this string using while loop.

    
    # Consider the string
    string1 = 'sparkby examples - Python tutorials'
    print("String: ",string1)
    
    it = 0   
    while (string1[it:]):
            it = it + 1
    
    # Length of string
    print("String Length: ",it)
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    

    We can see that there are total 35 characters present in the string including spaces.

    We can also implement using user-defined function.

    
    # Consider the string
    string1 = "sparkby examples - Python tutorials"
    print("String: ",string1)
    
    def length_fun(string1):
      it = 0   
      while (string1[it:]):
            it = it + 1
      return it
    
    # Length of string
    print("String Length: ",length_fun(string1))
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    

    <a id="h-5-string-length-using-sum"></a>

    5. String length using sum()

    In this scenario, we will use for loop inside sum() function and we have to pass a Boolean value-True or 1 before it. With this, total characters will be returned.

    <a id="h-5-1-syntax-structure"></a>

    5.1 Syntax Structure

    
    sum(True for i in string1)
                Or
    sum(1 for i in string1)
    

    <a id="h-5-2-string-length-with-sum-example"></a>

    5.2 String length with sum() example

    In this example, we will create a string and return the length of this string using sum() function.

    
    # Consider the string
    string1 = 'sparkby examples - Python tutorials'
    print("String: ",string1)
    
    # Length of string
    print("String Length: ",sum(True for i in string1))
    
    # Output:
    # String:  sparkby examples - Python tutorials
    # String Length:  35
    
    # Length of string
    print("String Length: ",sum(1 for i in string1))
    
    # Output:
    # String Length:  35
    

    We can see that there are total 35 characters present in the string including spaces.

    <a id="h-6-conclusion"></a>

    6. Conclusion

    So in this article, we seen how to return the length of the string using len() function, for loop, while loop and with sum() function. It can be possible to implement the above scenarios using user-defined functions. I have shown this in for loop and while loop scenarios.

    Happy learning!!

    <a id="h-related-articles"></a>

    Related Articles