0
0
Pandasdata~20 mins

String type (object, string) in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pandas string operation?

Given the following DataFrame, what will be the output of df['name'].str.upper()?

Pandas
import pandas as pd
df = pd.DataFrame({'name': ['alice', 'Bob', 'CHARLIE']})
result = df['name'].str.upper()
print(result)
A
0      alice
1        bob
2    charlie
Name: name, dtype: object
B
0      ALICE
1        BOB
2    CHARLIE
Name: name, dtype: object
C
0      ALICE
1        bob
2    CHARLIE
Name: name, dtype: object
D
0      Alice
1        Bob
2    Charlie
Name: name, dtype: object
Attempts:
2 left
💡 Hint

The str.upper() method converts all characters in the string to uppercase.

data_output
intermediate
2:00remaining
How many entries contain the substring 'cat'?

Using the DataFrame below, how many rows have the substring 'cat' in the animal column?

Pandas
import pandas as pd
df = pd.DataFrame({'animal': ['cat', 'dog', 'caterpillar', 'bird', 'concatenate']})
count = df['animal'].str.contains('cat').sum()
print(count)
A2
B1
C4
D3
Attempts:
2 left
💡 Hint

Check which strings include the exact substring 'cat' anywhere inside.

🔧 Debug
advanced
2:00remaining
What error does this code raise?

Consider this code snippet:

import pandas as pd
df = pd.DataFrame({'text': ['apple', 'banana', None]})
df['text'].str.len()

What error or output occurs?

Pandas
import pandas as pd
df = pd.DataFrame({'text': ['apple', 'banana', None]})
result = df['text'].str.len()
print(result)
A
0    5
1    6
2    0
dtype: int64
BTypeError: object of type 'NoneType' has no len()
C
0    5
1    6
2    NaN
dtype: float64
DAttributeError: 'NoneType' object has no attribute 'str'
Attempts:
2 left
💡 Hint

pandas string methods handle None values gracefully by returning NaN.

visualization
advanced
2:00remaining
Which plot shows the correct count of string lengths?

Given this DataFrame, which plot correctly shows the count of each string length in the words column?

Pandas
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'words': ['cat', 'dog', 'bird', 'fish', 'elephant']})
lengths = df['words'].str.len()
counts = lengths.value_counts().sort_index()
counts.plot(kind='bar')
plt.show()
ABar chart with x-axis: [3,4,8], y-axis: [2,2,1]
BBar chart with x-axis: [3,4,5], y-axis: [1,3,1]
CBar chart with x-axis: [3,4,5], y-axis: [2,2,1]
DBar chart with x-axis: [3,4,8], y-axis: [1,3,1]
Attempts:
2 left
💡 Hint

Count how many words have length 3, 4, and 8.

🧠 Conceptual
expert
2:00remaining
Why prefer pandas string dtype over object dtype for text data?

Which reason best explains why using pandas string dtype is better than object dtype for text columns?

AString dtype supports missing values natively and has optimized string methods.
BObject dtype uses less memory and is faster for string operations.
CObject dtype allows vectorized string operations which string dtype does not.
DString dtype automatically converts all text to lowercase.
Attempts:
2 left
💡 Hint

Think about missing data handling and performance.