What this topic tests
- Index and slice strings with the end position excluded.
- Use methods such as split, join, strip, and replace correctly.
- Build readable output with f-strings and predict exact characters.
Common mistakes
- Forgetting that slicing excludes the stop index.
- Expecting a string method to mutate the original string. Strings are immutable.
- Ignoring spaces, newlines, or quote characters when the question asks for exact output.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming takes the first three characters of the shop name "Pythonmilk tea" for a sign. What is "Pythonmilk tea"[0:3]?
Show answer
Answer
A. PytWhy: A slice includes the start index and excludes the end index, so [0:3] keeps indexes 0, 1 and 2: P, y, t. Index 3 would be the fourth character.
-
2. True or false: True and False are Python Boolean values, and their first letters must be capitalized.
Show answer
Answer
A. ✅ TrueWhy: Python's Boolean literals are True and False with a capital first letter. Writing true or false creates ordinary names and raises NameError.
-
3. 🧋 What is 'hello world'.split()?
Show answer
Answer
A. ['hello','world']Why: With no argument split() treats any run of whitespace as a single separator and also drops leading and trailing whitespace.
-
4. What happens with s = "abc"; s[0] = "A"?
Show answer
Answer
B. It raises TypeErrorWhy: Strings are immutable, so item assignment is rejected with TypeError. Build a new string with slicing, replace() or a join instead.
-
5. Ming formats a milk tea price with f"{3.14159:.2f}" yuan. What is the output?
Show answer
Answer
3.14Why: :.2f keeps two decimals and rounds, so 3.14159 becomes 3.14. The spec applies only to the value before the colon; the rest of the f-string stays literal.
-
6. 🧋 What type does '1,2,3'.split(',') return?
Show answer
Answer
listWhy: split() always returns a list, even when there is only one field. The elements are strings, so numeric fields still need conversion.
-
7. Replace the hyphens in an order with spaces. Which method completes the blank?
order = 'latte-large' print(order.____('-', ' '))Show answer
Answer
replaceWhy: replace('-', ' ') returns a new string with every hyphen swapped for a space. The original order string is unchanged because strings are immutable.
-
8. Ming truncates the shop name. What does this code print?
print("Python milk tea"[:3])Show answer
Answer
PytWhy: [:3] omits the start, so slicing begins at index 0 and stops before index 3; the space at index 6 is never reached. The stop index is exclusive, never included.
-
9. A debug label uses repr on the character with code 65. What is printed?
print(f"{chr(65)!r}")Show answer
Answer
'A'Why: chr(65) builds the character A, and !r applies repr, which wraps strings in quotes. That is why the output is 'A' with quotes around it.
-
10. What does `is_pangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog')` return?
from string import ascii_lowercase def is_pangram(sentence): return all(char in sentence.lower() for char in ascii_lowercase) print(is_pangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'))Show answer
Answer
FalseWhy: The digits 7 and 3 stand in for letters, so t and e never appear and all() returns False. Leetspeak does not count as a pangram.
Common questions
Why does s.strip() not change s?
Strings are immutable. strip returns a new string; assign it to a variable if you need to keep the result.
How do I join a list of strings?
Use separator.join(items). Every item must already be a string.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.