How to Find the Length of a String in Python
What you’ll build or solve
You’ll get the number of characters in a Python string and use that value in real code.
When this approach works best
Finding the length of a string works well when you:
Learn Python on Mimo
- Validate input, like checking a password length or a minimum message length.
- Trim or split text, then confirm the result has content before you use it.
- Track limits, like staying under a character cap for a username or a form field.
Skip this approach if you only need to check “empty or not.” In that case, if text: is often clearer than comparing len(text) to zero.
Prerequisites
- Python 3 installed
- You know what a string is
Step-by-step instructions
1) Get the length with len()
Use len() to get the number of characters in a string.
text="Hello"
count=len(text)
print(count)
What to look for: spaces and punctuation count as characters too.
2) Handle empty and whitespace-only strings
An empty string has length 0. A string full of spaces is not empty, so decide what you want to treat as “empty.”
Option A (common): check empty vs non-empty
text=""
iflen(text)==0:
print("Empty")
else:
print("Not empty")
Option B: treat whitespace-only as empty with .strip()
text=" "
iflen(text.strip())==0:
print("Empty after stripping whitespace")
else:
print("Has non-whitespace characters")
What to look for: .strip() removes leading and trailing whitespace, including spaces, tabs, and newlines.
Examples you can copy
Example 1: Enforce a minimum length
Bash
password="secret123"
iflen(password)<8:
print("Too short")
else:
print("OK")
Example 2: Check a max character limit
username="amina_92"
max_len=12
iflen(username)>max_len:
print("Too long")
else:
print("OK")
Example 3: Count characters after cleaning input
raw=" Hello world\n"
clean=raw.strip()
print(len(clean))
Example 4: Compare lengths of multiple strings
words= ["short","medium text","long"]
max_len=max(len(w)forwinwords)
print(max_len)
Example 5: Find the length of each word
sentence="Code is fun"
words=sentence.split()
lengths= [len(w)forwinwords]
print(lengths)
Common mistakes and how to fix them
Mistake 1: Using len() to check for “empty” when whitespace should count as empty
What you might do:
text=" "
iflen(text)==0:
print("Empty")
else:
print("Not empty")
Why it breaks: the string contains spaces, so its length is not zero.
Correct approach:
text=" "
iflen(text.strip())==0:
print("Empty after stripping whitespace")
else:
print("Has non-whitespace characters")
Mistake 2: Forgetting that spaces and newlines count
What you might do:
text="Hi\n"
print(len(text))
Why it surprises you: \n is a character, so it increases the count.
Correct approach: strip if you don’t want trailing whitespace to count.
text="Hi\n"
print(len(text.strip()))
Mistake 3: Calling len() on None
What you might do:
CSS
text=None
print(len(text))
Why it breaks: None has no length, so Python raises TypeError.
Correct approach: decide how you want to handle missing values.
Strict handling:
CSS
text=None
iftextisNone:
print("Missing")
else:
print(len(text))
Treat missing as empty:
Python
text=None
text=textor""
print(len(text))
Troubleshooting
If you see TypeError: object of type ... has no len(), you passed a non-string value to len(). Check type(text) and convert to str if needed.
If your string length seems too large, look for hidden whitespace like \n or \t. Use repr(text) to reveal hidden characters.
If limits feel inconsistent for emojis, you are hitting Unicode edge cases. len() counts Unicode code points, not user-perceived characters. Some emojis combine multiple code points into one visible symbol. For most apps, len() is still correct. If you need grapheme-aware counting, use a specialized library.
If you get unexpected results after reading a file, the string may include a trailing newline. Use .rstrip("\n") or .strip() depending on your needs.
Quick recap
- Use
len(text)to get the number of characters in a string. - An empty string has length
0. - Spaces, tabs, and newlines count as characters unless you strip them.
- Use
.strip()when whitespace-only input should count as empty. - Decide how to handle
Nonebefore callinglen().
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot