Bird
Raised Fist0
Data Analysis Pythondata~20 mins

String methods on Series in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Series Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of lowercasing a Series with mixed case strings
What is the output of this code?
import pandas as pd
s = pd.Series(['Apple', 'Banana', 'Cherry'])
result = s.str.lower()
print(result)
Data Analysis Python
import pandas as pd
s = pd.Series(['Apple', 'Banana', 'Cherry'])
result = s.str.lower()
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.lower() method converts all characters in each string to lowercase.
❓ data_output
intermediate
2:00remaining
Count of strings containing a substring in a Series
Given this Series, what is the output of counting how many strings contain 'an'?
import pandas as pd
s = pd.Series(['banana', 'apple', 'mango', 'orange'])
count = s.str.contains('an').sum()
print(count)
Data Analysis Python
import pandas as pd
s = pd.Series(['banana', 'apple', 'mango', 'orange'])
count = s.str.contains('an').sum()
print(count)
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Check which strings have 'an' inside them.
🔧 Debug
advanced
2:00remaining
Identify the error in using str.replace with regex
What error does this code raise?
import pandas as pd
s = pd.Series(['cat', 'bat', 'rat'])
result = s.str.replace('[ab]', '', regex=False)
print(result)
Data Analysis Python
import pandas as pd
s = pd.Series(['cat', 'bat', 'rat'])
result = s.str.replace('[ab]', '', regex=False)
print(result)
ATypeError: cannot use regex pattern when regex=False
BValueError: regex argument must be True or False
CTypeError: expected string or bytes-like object
D
No error, output is:
0    ct
1    bt
2    rt
dtype: object
Attempts:
2 left
💡 Hint
Check the meaning of regex=False when the pattern looks like a regex.
❓ visualization
advanced
2:00remaining
Visualize the count of string lengths in a Series
Which option shows the correct bar plot code to visualize the count of string lengths in this Series?
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'cat', 'elephant', 'fox'])
lengths = s.str.len()
# Which code below plots counts of each length?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'cat', 'elephant', 'fox'])
lengths = s.str.len()
A
lengths.value_counts().sort_index().plot(kind='bar')
plt.show()
B
lengths.plot(kind='bar')
plt.show()
C
s.str.len().plot(kind='line')
plt.show()
D
lengths.hist()
plt.show()
Attempts:
2 left
💡 Hint
We want counts of each length, not the lengths themselves.
🚀 Application
expert
3:00remaining
Filter Series for strings starting and ending with vowels
Given this Series, which option correctly filters strings that start and end with a vowel?
import pandas as pd
s = pd.Series(['apple', 'banana', 'orange', 'umbrella', 'grape', 'avocado'])
Data Analysis Python
import pandas as pd
s = pd.Series(['apple', 'banana', 'orange', 'umbrella', 'grape', 'avocado'])
A
filtered = s[s.str.match('^[aeiou].*[aeiou]$')]
print(filtered)
B
filtered = s[s.str.contains('^[aeiou].*[aeiou]$')]
print(filtered)
C
filtered = s[s.str.startswith(('a','e','i','o','u')) & s.str.endswith(('a','e','i','o','u'))]
print(filtered)
D
filtered = s[s.str.match('^[aeiou].*[aeiou]')]
print(filtered)
Attempts:
2 left
💡 Hint
Check how to combine startswith and endswith for multiple letters.

Practice

(1/5)
1. What does the .str.lower() method do when applied to a pandas Series?
easy
A. Converts all characters in each string of the Series to lowercase.
B. Removes all whitespace from each string in the Series.
C. Counts the number of characters in each string of the Series.
D. Replaces all digits in each string with a special character.

Solution

  1. Step 1: Understand the .str accessor

    The .str accessor allows string methods to be applied element-wise on a pandas Series.
  2. Step 2: Identify the effect of .lower()

    The .lower() method converts all uppercase letters in each string to lowercase.
  3. Final Answer:

    Converts all characters in each string of the Series to lowercase. -> Option A
  4. Quick Check:

    .str.lower() = lowercase conversion [OK]
Hint: Remember .str.lower() makes all letters small [OK]
Common Mistakes:
  • Confusing .str.lower() with .str.strip()
  • Thinking it removes whitespace
  • Assuming it counts characters
2. Which of the following is the correct syntax to check if each string in a pandas Series names contains the substring 'data'?
easy
A. names.contains(str('data'))
B. names.contains('data')
C. names.str.contains(data)
D. names.str.contains('data')

Solution

  1. Step 1: Recall the correct usage of string methods on Series

    String methods must be accessed via the .str accessor on a pandas Series.
  2. Step 2: Check the correct method call syntax

    The method .contains() requires the substring as a string argument inside quotes.
  3. Final Answer:

    names.str.contains('data') -> Option D
  4. Quick Check:

    Use .str.contains('substring') for substring check [OK]
Hint: Always use .str before string methods on Series [OK]
Common Mistakes:
  • Omitting .str accessor
  • Passing variable without quotes
  • Using .contains() directly on Series
3. Given the pandas Series fruits = pd.Series(['Apple', 'banana', 'Cherry', 'date']), what is the output of fruits.str.upper()?
medium
A. ['apple', 'banana', 'cherry', 'date']
B. ['APPLE', 'banana', 'CHERRY', 'DATE']
C. ['APPLE', 'BANANA', 'CHERRY', 'DATE']
D. ['Apple', 'banana', 'Cherry', 'date']

Solution

  1. Step 1: Understand .str.upper() effect on Series

    The .str.upper() method converts all characters in each string to uppercase.
  2. Step 2: Apply .str.upper() to each element

    Each fruit name is converted fully to uppercase: 'Apple' -> 'APPLE', 'banana' -> 'BANANA', etc.
  3. Final Answer:

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

    .str.upper() = all uppercase strings [OK]
Hint: Uppercase method makes all letters capital [OK]
Common Mistakes:
  • Thinking it only affects first letter
  • Confusing with .str.capitalize()
  • Expecting original case unchanged
4. What is wrong with this code snippet?
import pandas as pd
s = pd.Series(['cat', 'dog', 'bird'])
s.str.replace('d', '')
medium
A. It raises a FutureWarning and requires regex=false to avoid it.
B. It works correctly and removes 'd' from each string.
C. It raises a TypeError because 'replace' is not a string method.
D. It returns null instead of a Series.

Solution

  1. Step 1: Understand .str.replace() default behavior

    By default, .str.replace() treats the first argument as a regex pattern.
  2. Step 2: Identify warning about regex default change

    Recent pandas versions raise a FutureWarning suggesting to specify regex=false when replacing literal strings.
  3. Final Answer:

    It raises a FutureWarning and requires regex=false to avoid it. -> Option A
  4. Quick Check:

    .str.replace() warns about regex=true default [OK]
Hint: Add regex=false to .str.replace() for literal replace [OK]
Common Mistakes:
  • Ignoring FutureWarning
  • Assuming no warning occurs
  • Expecting error instead of warning
5. You have a pandas Series emails = pd.Series(['John.Doe@example.com', 'jane_smith@work.net', 'admin@site.org']). How can you extract the domain names (like 'example.com') from each email using string methods?
hard
A. emails.str.extract('(@.*)')
B. emails.str.split('@').str[1]
C. emails.str.replace('.*@', '')
D. emails.str.slice(start=emails.str.find('@')+1)

Solution

  1. Step 1: Understand the goal to get text after '@'

    We want the part of each string after the '@' symbol, which is the domain.
  2. Step 2: Use .str.split('@') and select the second part

    Splitting each string by '@' creates a list; index 1 is the domain part.
  3. Final Answer:

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

    Split by '@' and take second element [OK]
Hint: Split by '@' and pick index 1 for domain [OK]
Common Mistakes:
  • Using incorrect regex in .str.extract()
  • Forgetting to select index after split
  • Using .str.replace() incorrectly
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