How to Get the Length of a String in Python?

As a Python developer working on a project for one of my clients, I recently faced an issue where I needed to determine the number of characters in a string. After researching and testing various methods, I discovered several ways to accomplish this task. In this tutorial, I will explain how to get the length of a string in Python. I will share my findings and provide detailed examples to help you understand better.

Get the Length of a String in Python

Let us learn some important methods to get the length of a string in Python.

Read How to Concatenate String and Int in Python?

Method 1: Use the len() Function

The len() function in Python returns the number of characters in a string.

Example:

# Using len() to find the length of a string
city = "San Francisco"
length = len(city)
print(f"The length of '{city}' is {length}.")

Output:

The length of 'San Francisco' is 13.

I executed the above example code and added the screenshot below.

Get the Length of a String in Python

Using the len() Function: This built-in function provides the most simple and efficient way to obtain the length of a string.

Check out How to Convert Datetime to String in Python?

Method 2: Iterate with a for Loop and the in Operator

By iterating through each character in the Python string, you can manually count the number of characters.

Example:

# Finding length by iterating through the string
state = "California"
count = 0
for char in state:
    count += 1
print(f"The length of '{state}' is {count}.")

Output:

The length of 'California' is 10.

I executed the above example code and added the screenshot below.

How to Get the Length of a String in Python

Iterating with a for Loop and the in Operator: By manually counting each character during iteration, you can determine the string’s length without relying on built-in functions.

Read How to Repeat a String Multiple Times in Python?

Method 3: Combine join() and count() Methods

This method involves joining the Python string with an empty separator and counting the occurrences of the separator.

Example:

# Using join() and count() to determine string length
president = "George Washington"
length = ''.join(president).count('') - 1
print(f"The length of '{president}' is {length}.")

Output:

The length of 'George Washington' is 17.

I executed the above example code and added the screenshot below.

Get the Length of a String in Python join() and count()

Combining join() and count() Methods: This approach involves joining the string with an empty separator and counting the occurrences of the separator to find the length.

Check out How to Split String by Whitespace in Python?

Method 4: Utilize str.count()

The str.count() method in Python can count the occurrences of a specific substring. By counting the empty string '' you can determine the length.

Example

# Using str.count() to find the length of a string
landmark = "Statue of Liberty"
length = landmark.count('') - 1
print(f"The length of '{landmark}' is {length}.")

Output:

The length of 'Statue of Liberty' is 16.

Utilizing str.count(): By counting the occurrences of an empty string within the target string and adjusting the count, you can ascertain its length.

Read How to Convert a Comma-Separated String to a List in Python?

Conclusion

In this article, I explored g four different methods to get the length of a string in Python, such as using len() function, Iterating with a for loop and the in operator, combining join() and count() methods, and utilizing str.count().

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.