How to Get the Length of a String in Python

Techwalla may earn compensation through affiliate links in this story.

Python is a fast and powerful open-source, interpretive programming language with extensive standard libraries. When programming in Python, you may need to find the length of a string. For example, you may want to evaluate the amount of characters that a user enters in a text field, and return an error if the text is too long. Using the built-in "len" function, you can quickly calculate the length of a string.


Step 1

Access your Python editor.

Video of the Day

Step 2


exampleString = 'This is my example string' len(exampleString);


Step 3

Press "Enter."

In this example, Python reads the exampleString and calculates the number of characters in the string using the "len" function. In this example, Python returns '25.'


Note that when calculating the length, Python counts all characters -- including white spaces and special characters.



Report an Issue

screenshot of the current page

Screenshot loading...