0
0
Pandasdata~20 mins

str.len() for string length in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Length Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of str.len() on a pandas Series
What is the output of the following code snippet?
Pandas
import pandas as pd
s = pd.Series(['apple', 'banana', 'cherry', None])
result = s.str.len()
print(result)
A
0    5
1    6
2    6
3    None
dtype: object
B
0    5
1    6
2    6
3    0
dtype: int64
C
0    5.0
1    6.0
2    6.0
3    NaN
dtype: float64
D
0    5
1    6
2    6
3    NaN
dtype: float64
Attempts:
2 left
💡 Hint
Remember that missing values in pandas string operations usually result in NaN, not zero or None.
data_output
intermediate
1:30remaining
Count of strings with length greater than 4
Given the Series below, what is the count of strings with length greater than 4?
Pandas
import pandas as pd
s = pd.Series(['dog', 'elephant', 'cat', 'lion', 'tiger', None])
count = (s.str.len() > 4).sum()
print(count)
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Check which strings have length more than 4 and remember None values are ignored in comparison.
visualization
advanced
2:00remaining
Plot string length distribution
You have a pandas Series of strings. Which code snippet correctly plots a histogram of string lengths?
Pandas
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['red', 'blue', 'green', 'yellow', 'purple', 'orange'])
# Fill in the missing code to plot histogram of string lengths
A
plt.hist(s.str.length())
plt.show()
B
plt.hist(s.len())
plt.show()
C
plt.hist(len(s))
plt.show()
D
plt.hist(s.str.len())
plt.show()
Attempts:
2 left
💡 Hint
Use the pandas string accessor to get lengths before plotting.
🔧 Debug
advanced
1:30remaining
Identify the error in string length calculation
What error will the following code raise?
Pandas
import pandas as pd
s = pd.Series(['a', 'bb', 'ccc'])
lengths = s.len()
print(lengths)
AAttributeError: 'Series' object has no attribute 'len'
BTypeError: 'Series' object is not callable
CNo error, prints lengths
DValueError: Length mismatch
Attempts:
2 left
💡 Hint
Check if 'len' is a direct method of Series or accessed differently.
🚀 Application
expert
2:30remaining
Filter DataFrame rows by string length in a column
Given a DataFrame df with a column 'Name', which code correctly filters rows where the 'Name' length is exactly 5 characters?
Pandas
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve']})
# Fill in the code to filter rows with Name length 5
A
filtered = df[df['Name'].apply(len) == 5]
print(filtered)
B
filtered = df[df['Name'].str.len() == 5]
print(filtered)
C
filtered = df[df['Name'].len() == 5]
print(filtered)
D
filtered = df[df['Name'].str.length() == 5]
print(filtered)
Attempts:
2 left
💡 Hint
Use the pandas string accessor for vectorized string length operations.