All sample questions / pandas
What does print(s.str.startswith('a').tolist()) print? pandas Strings, Dates and apply
Block 8, Strings, Dates and apply, question 19 of the pandas sample
import pandas as pd
s = pd.Series(['apple', 'banana', 'avocado'])
print(s.str.startswith('a').tolist())
What does this print?
Show the answer and why each option is right or wrong
Answer, C. [True, False, True]
- A. [True, True, True]
- Assumes startswith checks for the letter anywhere in the string instead of only at its very first position.
- B. ['a', 'a']
- Assumes startswith extracts the matching prefix itself instead of reporting a boolean for each row.
- C. [True, False, True]
- Correct. str.startswith checks only the first character of each string against 'a', which apple and avocado satisfy and banana does not.
- D. [False, True, False]
- Reports the exact opposite of each row, as if the check were for endswith or a negated condition.
str.startswith mirrors the built in str.startswith method, testing only the beginning of each string, and it returns one boolean per row rather than extracting any part of the text.
The answer above is what pandas 3.0.5 printed when the snippet was run, not what someone expected it to do.
This question comes from a bank of 255 pandas interview questions, all built the same way, sold as one PDF with the full answer key.
Get the 255 pandas questions, 9 euros
Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros
Or play the free pandas sample, twenty questions in the browser, also as a free PDF.
Previous question Next question
Did this save you a lookup? Buy me a coffee on Ko-fi.