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.

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.

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.

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:
- How to Remove Characters from a String in Python?
- How to Remove HTML Tags from a String in Python?
- How to Convert an Object to a String in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.