🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

Series.str.len()

AI & DATA SCIENCE // series-str-len

series.str.len() returns the character length of each string in a Series, the vectorized equivalent of Python's built-in len() applied to every element.

Syntax

series.str.len()

Deep Dive Course

str.len() computes the number of characters in each string element, returning an integer Series, with NaN, as a float, for any missing entries, and it's commonly used to filter or flag text that's unexpectedly too short or too long, like validating that a code or ID field always has an exact expected length.

1Understanding Series.str.len()

str.len() computes the number of characters in each string element, returning an integer Series, with NaN, as a float, for any missing entries, and it's commonly used to filter or flag text that's unexpectedly too short or too long, like validating that a code or ID field always has an exact expected length.

💡

Combine str.len() with boolean indexing to quickly find rows where a text field doesn't have its expected fixed length, a common data-validation check.

editor.html
import pandas as pd

s = pd.Series(["cat", "elephant", "dog"])
print(s.str.len())
localhost:3000

2Practical Example

Here is a real-world application of Series.str.len() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"code": ["AB123", "CD45", "EF678"]})
invalid = df[df["code"].str.len() != 5]
print(invalid)
localhost:3000

3Best Practices

Follow these guidelines when working with Series.str.len():

1. Use str.len() combined with boolean indexing to validate that a text field consistently has an expected length, like a fixed-format code or ID

2. Use str.len() instead of applying Python's len() with apply(), for the same vectorized-performance reasons that favor other .str methods over apply()

3. Remember str.len() returns NaN, not 0, for missing values, since there's no string at all to measure the length of

⚠️

Tip: Combine str.len() with boolean indexing to quickly find rows where a text field doesn't have its expected fixed length, a common data-validation check.

editor.html
import pandas as pd

s = pd.Series(["cat", "elephant", "dog"])
print(s.str.len())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series(["cat", "elephant", "dog"])
print(s.str.len())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"code": ["AB123", "CD45", "EF678"]})
invalid = df[df["code"].str.len() != 5]
print(invalid)

Best Practices

  • Use str.len() combined with boolean indexing to validate that a text field consistently has an expected length, like a fixed-format code or ID
  • Use str.len() instead of applying Python's len() with apply(), for the same vectorized-performance reasons that favor other .str methods over apply()
  • Remember str.len() returns NaN, not 0, for missing values, since there's no string at all to measure the length of

Interview Question

Why does series.str.len() return NaN, rather than 0, for a missing (NaN) element in the Series?

Hint: Think about the conceptual difference between a string of length zero and no string at all.

A NaN entry represents the complete absence of a string value, not the presence of an actual empty string — an empty string genuinely has a length of 0, but a missing entry has no string to measure at all, which is a fundamentally different situation. Returning NaN for it preserves that important distinction between this row's text field being blank versus this row's text field being missing entirely, which returning 0 for both cases would incorrectly conflate.

Exercises

MediumPractice using Series.str.len() in a real scenario.
View Solution
import pandas as pd

s = pd.Series(["cat", "elephant", "dog"])
print(s.str.len())

Frequently Asked Questions

Why does series.str.len() return NaN, rather than 0, for a missing (NaN) element in the Series?

A NaN entry represents the complete absence of a string value, not the presence of an actual empty string — an empty string genuinely has a length of 0, but a missing entry has no string to measure at all, which is a fundamentally different situation. Returning NaN for it preserves that important distinction between this row's text field being blank versus this row's text field being missing entirely, which returning 0 for both cases would incorrectly conflate.

Related Functions

series-str-containsdf-isnalen()