In this Python tutorial, you will learn how to find the length of a string using the built-in len() function. You will also see how spaces, newline characters, Unicode characters, and empty strings affect the result.
Find the Length of a String in Python
To find the length of a string in Python, pass the string to the built-in len() function.
The function returns an integer representing the number of characters in the string. Letters, digits, spaces, punctuation marks, and escape characters such as \n are included in the count.
Syntax of len() for a Python String
The syntax of len() for finding string length is:
len(string)
Here, string is the string object whose length you want to calculate. The return value is an integer greater than or equal to zero.
Python String Length Examples
1. Find the Length of a String
In the following example, we store a string in a variable, pass it to len(), and print the returned length.
Python Program
str = "Welcome to TutorialKart."
length = len(str)
print("Length of the string is :", length)
Output
Length of the string is : 24
The result is 24 because the count includes letters, spaces, and the period at the end of the string.
2. Find the Length of an Empty String
An empty string contains no characters, so its length is zero.
Python Program
str = ""
length = len(str)
print("Length of the string is :", length)
Output
Length of the string is : 0
3. Count Spaces in a Python String
Spaces are characters and are included in the value returned by len().
text = "Python string"
print(len(text))
Output
13
The string contains six letters in Python, one space, and six letters in string.
4. Find String Length After Removing Leading and Trailing Spaces
If leading or trailing spaces should not be counted, remove them with strip() before calling len().
text = " Python "
print(len(text))
print(len(text.strip()))
Output
10
6
The first result includes all four surrounding spaces. The second result counts only the characters in Python.
5. Count Newline and Tab Characters
Escape sequences that represent characters, such as a newline \n or a tab \t, each contribute one character to the string length.
text = "Python\nTutorial"
print(len(text))
print(text)
Output
15
Python
Tutorial
The newline character is included in the length even though it is displayed as a line break.
6. Find the Length of a Unicode String
Python strings support Unicode. For many common Unicode characters, len() counts each character as one item.
text = "Python ✓"
print(len(text))
Output
8
The result includes six letters, one space, and the check mark character.
What Python len() Counts in a String
| String content | Counted by len()? | Notes |
|---|---|---|
| Letters and digits | Yes | Each character contributes to the length. |
| Spaces | Yes | Leading, trailing, and internal spaces are counted. |
| Punctuation | Yes | Characters such as commas and periods are included. |
| Newline and tab characters | Yes | Each escape sequence represents a character in the resulting string. |
| Empty string | No characters | len("") returns 0. |
String Length, Byte Length, and Encoded Text
len() applied to a Python string returns the number of string elements, not the number of bytes used after encoding.
To find the byte length of encoded text, encode the string first and then call len() on the resulting bytes object.
text = "café"
character_count = len(text)
byte_count = len(text.encode("utf-8"))
print(character_count)
print(byte_count)
Output
4
5
The string has four characters, but its UTF-8 representation uses five bytes because the accented character requires more than one byte.
Common Errors When Finding Python String Length
Calling len() on None
None is not a string and has no length. Passing it to len() raises a TypeError.
value = None
print(len(value))
Output
TypeError: object of type 'NoneType' has no len()
Check the value before calling len() when a variable may contain None.
value = None
length = len(value) if value is not None else 0
print(length)
Output
0
Avoid Using str as a Variable Name
The existing examples use str as a variable name. Although this works, str is also the name of Python’s built-in string type. In new programs, use names such as text, message, or value to avoid shadowing the built-in name.
Frequently Asked Questions About Python String Length
Does len() count spaces in a Python string?
Yes. Every space in the string is counted, including spaces at the beginning, middle, and end.
How do I find the length of a string without surrounding spaces?
Call strip() before len(), as in len(text.strip()). This removes leading and trailing whitespace before counting.
What does len() return for an empty string?
len("") returns 0 because the string contains no characters.
Does len() return the number of bytes in a string?
No. For a string, len() returns its character count. To count encoded bytes, use an expression such as len(text.encode("utf-8")).
Python String Length Summary
Use len(string) to find the length of a string in Python. The result includes spaces, punctuation, newline characters, and other characters stored in the string. An empty string has a length of zero. When you need the length after removing surrounding whitespace, use len(string.strip()). To measure encoded byte length instead of string length, encode the text before calling len().
In this Python Tutorial, we learned how to find the length of a string in Python using len() function.
TutorialKart.com