How to get the Python string length [5 Methods]

In this Python tutorial, we will learn various methods to get the Python string length. We’ll use some built-in functions to understand different approaches to get the length of a string in Python with some illustrative examples.

As a Developer, while making the Python Project I got the requirement to find the length of a string in Python. And, I came to know some methods that can help us to get the Python string length.

To get the Python string length as output, Python has five simple methods like the len() function that returns the length in number, or we can iterate over the string with a for loop and count the length of the Python string, or can use string slicing with the loops like for or while and get the length of string, or can we separate each and every characters of the string and then count them with count() function and then join them back again with the join() function.

Get the Python string length methods

Let’s list the methods and understand them in detail to get the Python string length methods.

  • The len() function
  • Using for loop
  • The for loop with slicing
  • Using join() with count() function
  • while loop and slicing

Let’s see all of them one by one using some demonstrative examples:

Method 1: How to get the length of a string in Python using len()

  • In this section, we will discuss how to find the length of a string in Python using len().
  • Python has a built-in function called len(). To determine the length of a given string, array, list, tuple, dictionary, etc., use the len() function. Len function can be used to enhance program performance. Len assists in providing the number of elements because the number of elements stored in the object is never determined.

Syntax:

Here is the Syntax of the len() function in Python

len(value)

Note: This parameter takes only one parameter that shows that you want the find the length of a string.

Example:

Let’s take an example and check how to get the length of a string in Python using len().

Source Code:

# Input string
new_str = "Python is a Programming language"
# Display the Content
print("The length of the input string  is :", len(new_str))

Output: In the following given code first, we created a string, and then we checked the length of the string by using the len() function.

The length of the input string  is : 32

Here is the implementation of the following given Python code:

How to get the Python string length

This is how to get the Python string length using the len() function.

Method 2: How to find the length of a string in Python using for loop

  • Now let us understand how to find the length of a string in Python using a for loop.
  • To perform this particular task we are going to use a for loop Instead of using the len() function, you can determine the length by iterating the string through a for-loop while increasing a counter variable each time.

Example:

Let’s take an example and check how to find the length of a string in Python using for loop.

new_count = 0
for i in "USA": 
    new_count+=1
print (new_count)

Output: In the above code first, we declared a variable ‘new_count’ and then length by iterating the string through a for loop while increasing a counter variable each time.

The length of the string is: 3

Here is the Screenshot of the following given code.

length of a string in python

As you can see in the Screenshot we have discussed how to get the Python string length using for loop.

Method 3: check string length Python using a for loop and slicing

  • In this section, we will discuss how to find the length of a string in Python using a loop and slicing.
  • The for loop and the slicing concept will be used in this method. Until we reach an empty string, we will slice a string in half, reducing its length by 1 each time. terminate the for loop. The length of the string will depend on how many times the loop has been repeated.

Example:

Here we will take an example and check how to find the length of a string in Python using a loop and slicing.

Source Code:

def str(x):
    z = 0
    for i in x:
        z += 1
    return z
x= "China, England, Germany"
print("Size of string: ",str(x))

Output: Here, the z is the counter that keeps the count of the characters in the Python string while we iterate over the string.

Size of string:  23

You can refer to the below Screenshot.

python string length

In this example, we have understood how to get the Python string length using a loop and slicing.

Method 4: How to get a length of a string in Python using join() with count() function

  • In this section, we will discuss how to find the length of a string in Python using join() with count().
  • To get the length of a string, we will combine the join() and count() methods in one function. The concatenation of two strings is what the join() function will return. The number of characters in a string is returned by the count() function.
  • The given string and a new empty string will be concatenated using the join() method, and we’ll then count the characters in the new string. We will deduct 1 from the final count because the count() method counts the empty string as space and adds an extra count of 1.

Example:

def findLen(s):
    if not s:
        return 0
    else:
        st= ''
        return ((st).join(s)).count(st)-1
        
s= "This"
print("Size of string: ",findLen(s))

Output: Here is the implementation of the following given code.

def findLen(s):
    if not s:
        return 0
    else:
        st = ''
        return ((st).join(s)).count(st) - 1
s = "This"
print("Size of string: ", findLen(s))
string length python

This is how to get the Python string length using join() and count() functions.

Method 5: length of string Python using a while loop and slicing

  • Here we will discuss how to fetch the length of a string in Python using a while loop and slicing.
  • At some point, an empty string will result from repeatedly breaking a string in half, removing one character at a time. At this point, the while loop ends.

Example:

def new_len(Cars_in_usa):
    i = 0
    while Cars_in_usa[i:]:
        i += 1
    return i
Cars_in_usa = "BMW, VOLKSWAGEN, AUDII"
print("The lenght of the string is: ", new_len(Cars_in_usa))

Output:

The lenght of the string is:  22

Here is the Screenshot of the following given Python code:

python length of string

This way we can use a while loop with slicing to get the Python string length.

Conclusion

This article explains how to get the Python string length using five different methods such as len() function, for loop, or with slicing, or join() with count() function, and while loop with list slicing. Understanding all the methods with demonstrative examples can help one to select the techniques according to their need.

You may also like to read the following Python tutorials.