How to Use str.len in pandas to Get String Lengths
Use
Series.str.len() in pandas to get the length of each string element in a Series or DataFrame column. It returns a new Series with the count of characters for each string, handling missing values safely.Syntax
The basic syntax to get string lengths in pandas is:
Series.str.len()Here:
Seriesis a pandas Series containing string values.straccesses string methods for the Series.len()returns the length of each string element.
python
series.str.len()
Example
This example shows how to use str.len() on a pandas Series to find the length of each string:
python
import pandas as pd # Create a Series with string values series = pd.Series(['apple', 'banana', 'kiwi', None, 'orange']) # Get length of each string lengths = series.str.len() print(lengths)
Output
0 5
1 6
2 4
3 NaN
4 6
dtype: object
Common Pitfalls
Common mistakes when using str.len() include:
- Applying
str.len()on non-string data without converting, which causes errors. - Not handling
NaNorNonevalues, which returnNaNin the result. - Trying to use
len()directly on a Series instead ofstr.len().
Correct usage ensures safe handling of missing values and works only on string data.
python
import pandas as pd # Wrong: Using len() on Series (raises error) # series = pd.Series(['a', 'bb', 'ccc']) # lengths = len(series) # This returns total length of Series, not string lengths # Right: Use str.len() series = pd.Series(['a', 'bb', 'ccc', None]) lengths = series.str.len() print(lengths)
Output
0 1
1 2
2 3
3 NaN
dtype: object
Quick Reference
| Method | Description | Example Output |
|---|---|---|
| str.len() | Returns length of each string element | [5, 6, 4, NaN, 6] |
| str.upper() | Converts strings to uppercase | ['APPLE', 'BANANA', 'KIWI', NaN, 'ORANGE'] |
| str.lower() | Converts strings to lowercase | ['apple', 'banana', 'kiwi', NaN, 'orange'] |
| str.strip() | Removes whitespace from ends | ['apple', 'banana', 'kiwi', NaN, 'orange'] |
Key Takeaways
Use Series.str.len() to get the length of each string in a pandas Series.
str.len() safely handles missing values by returning NaN for them.
Do not use Python's len() on a Series to get string lengths; it returns the Series length instead.
Ensure your Series contains strings or convert data before using str.len().
str.len() is a quick way to analyze string length distributions in your data.