0
0
Data Analysis Pythondata~20 mins

String accessor (.str) methods in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Accessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of .str.upper() on a Series
What is the output of this code snippet?
Data Analysis Python
import pandas as pd
s = pd.Series(['apple', 'Banana', 'Cherry'])
result = s.str.upper()
print(result)
A
0    APPLE
1    Banana
2    CHERRY
dtype: object
B
0    apple
1    Banana
2    Cherry
dtype: object
C
0     APPLE
1    BANANA
2    CHERRY
dtype: object
D
0    apple
1    BANANA
2    cherry
dtype: object
Attempts:
2 left
💡 Hint
The .str.upper() method converts all characters in each string to uppercase.
data_output
intermediate
2:00remaining
Count of strings containing 'a' using .str.contains()
Given the Series below, what is the count of strings that contain the letter 'a' (case insensitive)?
Data Analysis Python
import pandas as pd
s = pd.Series(['Cat', 'dog', 'parrot', 'fish', 'ant'])
count = s.str.contains('a', case=False).sum()
print(count)
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Use .str.contains() with case=False to ignore letter case.
🔧 Debug
advanced
2:00remaining
Identify the error in .str.replace() usage
What error does this code raise?
Data Analysis Python
import pandas as pd
s = pd.Series(['apple', 'banana', 'cherry'])
result = s.str.replace('a', 1)
print(result)
ATypeError: expected string or bytes-like object
BValueError: invalid replacement
C
No error, output is:
0    1pple
1    b1n1n1
2    cherry
dtype: object
DAttributeError: 'Series' object has no attribute 'str'
Attempts:
2 left
💡 Hint
Check the type of the replacement argument in .str.replace().
🧠 Conceptual
advanced
2:00remaining
Behavior of .str.split() with n parameter
What is the output of this code?
Data Analysis Python
import pandas as pd
s = pd.Series(['a,b,c', 'd,e,f', 'g,h,i'])
result = s.str.split(',', n=1)
print(result)
A
0    [a, b, c]
1    [d, e, f]
2    [g, h, i]
dtype: object
B
0    [a]
1    [d]
2    [g]
dtype: object
C
0    [a,b,c]
1    [d,e,f]
2    [g,h,i]
dtype: object
D
0    [a, b,c]
1    [d, e,f]
2    [g, h,i]
dtype: object
Attempts:
2 left
💡 Hint
The n parameter limits the number of splits from the left.
🚀 Application
expert
3:00remaining
Extract domain names from email addresses
Given a Series of email addresses, which option correctly extracts the domain names (part after '@')?
Data Analysis Python
import pandas as pd
emails = pd.Series(['alice@example.com', 'bob@test.org', 'carol@data.net'])
domains = ???
print(domains)
Aemails.str.partition('@')[2]
Bemails.str.extract(r'@(\w+\.\w+)')[0]
Cemails.str.split('@').str[1]
Demails.str.replace(r'.*@', '')
Attempts:
2 left
💡 Hint
Use a method that splits the string at '@' and returns the part after it.