Compute the length of each string in a pandas Series with str.len()

Quick Overview

Write a pandas function that returns the character length of every string in a Series while keeping its index and name. It tests the vectorized string accessor, how missing values change the result dtype, and what pandas counts as a character.

Compute the length of each string in a pandas Series with str.len()

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `def string_lengths(s: pd.Series) -> pd.Series` that returns the character length of each string using `s.str.len()`. Requirements: - Preserve the input's `index` and `name`. - For the graded input (no NaN), the result dtype is `int64`. NaN would propagate and give `float64`. Examples: ```python s = pd.Series(['ab', 'abcd', 'x']) string_lengths(s) # 0 2 # 1 4 # 2 1 # dtype: int64 s = pd.Series(['ab', 'abcd', 'x'], index=[10, 20, 30], name='code') string_lengths(s) # 10 2 # 20 4 # 30 1 # Name: code, dtype: int64 ``` ```hint Before you rebuild anything Compare the index and name of the accessor's output with the input's before writing extra code to restore them. ``` ### What a Strong Answer Covers - The `.str.len()` call and why the index and name come through without extra code. - The result dtype with and without missing values. - How `.str.len()` treats missing or non-string elements compared with a plain `apply(len)`. - What "character length" counts for text outside plain ASCII. ### Follow-up Questions - If the Series contained `NaN`, what would the result look like, and how would you return integers anyway? - What does `s.apply(len)` do differently on the same input? - What does `str.len()` count for accented letters or emoji, and how would you count UTF-8 bytes instead?

Overview: Write a pandas function that returns the character length of every string in a Series while keeping its index and name. It tests the vectorized string accessor, how missing values change the result dtype, and what pandas counts as a character.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write def string_lengths(s: pd.Series) -> pd.Series that returns the character length of each string using s.str.len().

Requirements:

  • Preserve the input's index and name .
  • For the graded input (no NaN), the result dtype is int64 . NaN would propagate and give float64 .

Examples:

s = pd.Series(['ab', 'abcd', 'x'])
string_lengths(s)
# 0    2
# 1    4
# 2    1
# dtype: int64

s = pd.Series(['ab', 'abcd', 'x'], index=[10, 20, 30], name='code')
string_lengths(s)
# 10    2
# 20    4
# 30    1
# Name: code, dtype: int64

What a Strong Answer Covers Guidance

  • The .str.len() call and why the index and name come through without extra code.
  • The result dtype with and without missing values.
  • How .str.len() treats missing or non-string elements compared with a plain apply(len) .
  • What "character length" counts for text outside plain ASCII.

Follow-up Questions Guidance

  • If the Series contained NaN , what would the result look like, and how would you return integers anyway?
  • What does s.apply(len) do differently on the same input?
  • What does str.len() count for accented letters or emoji, and how would you count UTF-8 bytes instead?