Bird
Raised Fist0
Data Analysis Pythondata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the .str accessor do in pandas when used on a text column?
easy
A. It allows applying string methods to each element in the column.
B. It converts the column to numeric values.
C. It sorts the column alphabetically.
D. It removes missing values from the column.

Solution

  1. Step 1: Understand the purpose of .str in pandas

    The .str accessor is used to apply string methods element-wise on a pandas Series containing text data.
  2. Step 2: Compare options with the known function

    Only It allows applying string methods to each element in the column. correctly describes this behavior. Options A, B, and D describe other unrelated operations.
  3. Final Answer:

    It allows applying string methods to each element in the column. -> Option A
  4. Quick Check:

    .str applies string methods [OK]
Hint: Remember: .str works like string methods on each item [OK]
Common Mistakes:
  • Thinking .str changes data type
  • Confusing .str with sorting or filtering
  • Assuming it removes missing values automatically
2. Which of the following is the correct syntax to convert all strings in a pandas Series df['name'] to lowercase using .str?
easy
A. df['name'].str.toLower()
B. df['name'].lower()
C. df.str['name'].lower()
D. df['name'].str.lower()

Solution

  1. Step 1: Recall correct .str usage

    To apply string methods on a Series, use Series.str.method(). Here, lower() is the method to convert to lowercase.
  2. Step 2: Check each option's syntax

    df['name'].str.lower() uses df['name'].str.lower(), which is correct. df['name'].lower() misses .str. df.str['name'].lower() has wrong order. df['name'].str.toLower() uses incorrect method name toLower() (JavaScript style).
  3. Final Answer:

    df['name'].str.lower() -> Option D
  4. Quick Check:

    Use .str.lower() on Series [OK]
Hint: Always use .str before string methods on Series [OK]
Common Mistakes:
  • Omitting .str and calling string method directly
  • Using JavaScript method names like toLower()
  • Incorrect order like df.str['name']
3. Given the pandas Series:
import pandas as pd
s = pd.Series(['Apple', 'banana', 'Cherry'])
result = s.str.upper()

What is the value of result?
medium
A. ['APPLE', 'BANANA', 'CHERRY']
B. ['apple', 'banana', 'cherry']
C. ['Apple', 'banana', 'Cherry']
D. Error: str.upper() not found

Solution

  1. Step 1: Understand what .str.upper() does

    This method converts each string in the Series to uppercase letters.
  2. Step 2: Apply .str.upper() to each element

    'Apple' becomes 'APPLE', 'banana' becomes 'BANANA', and 'Cherry' becomes 'CHERRY'.
  3. Final Answer:

    ['APPLE', 'BANANA', 'CHERRY'] -> Option A
  4. Quick Check:

    .str.upper() converts all to uppercase [OK]
Hint: Uppercase all strings with .str.upper() [OK]
Common Mistakes:
  • Expecting lowercase output
  • Thinking original case remains unchanged
  • Assuming method causes error
4. What is wrong with this code snippet?
import pandas as pd
s = pd.Series(['cat', 'dog', 'bird'])
result = s.str.capitalize
medium
A. Series does not support .str accessor
B. Wrong method name, should be capital
C. Missing parentheses to call the method
D. No error, code runs fine

Solution

  1. Step 1: Identify the method call syntax

    String methods must be called with parentheses, e.g., capitalize(), not just referenced.
  2. Step 2: Check the code snippet

    The code uses s.str.capitalize without parentheses, so it returns a method object, not the transformed Series.
  3. Final Answer:

    Missing parentheses to call the method -> Option C
  4. Quick Check:

    Always add () to call string methods [OK]
Hint: Add () to call string methods, not just reference [OK]
Common Mistakes:
  • Forgetting parentheses after method name
  • Using wrong method names
  • Thinking Series lacks .str accessor
5. You have a pandas Series s with email addresses. You want to extract the domain part (after '@') for each email. Which code correctly uses .str methods to do this?
hard
A. s.str.replace('@', '')
B. s.str.split('@').str[1]
C. s.str.find('@')
D. s.str.extract('@')

Solution

  1. Step 1: Understand the goal

    We want the part after '@' in each email string.
  2. Step 2: Analyze each option

    s.str.split('@').str[1] splits each string at '@' and selects the second part (index 1), which is the domain. s.str.extract('@') extracts only '@' character, not the domain. s.str.find('@') returns the position of '@', not the domain. s.str.replace('@', '') removes '@' but keeps the rest combined.
  3. Final Answer:

    s.str.split('@').str[1] -> Option B
  4. Quick Check:

    Split by '@' and take second part [OK]
Hint: Split by '@' and select index 1 for domain [OK]
Common Mistakes:
  • Using extract('@') which only matches '@'
  • Using find('@') which returns position, not substring
  • Replacing '@' removes it but does not isolate domain
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard