Core syntax · 10 sample answers

Python strings: practice questions with answers.

String questions expose small mistakes quickly: an off-by-one slice, a method that returns a new string instead of changing the original, or a missing space in formatted output. These ten questions cover the patterns that matter most.

86 questions in the full bank · 21 choice · 9 fill · 56 output

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

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0108

    1. Ming takes the first three characters of the shop name "Pythonmilk tea" for a sign. What is "Pythonmilk tea"[0:3]?

    1. Pyt
    2. Pyth
    3. honmilk tea
    4. yth
    Show answer

    Answer

    A. Pyt

    Why: 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. Multiple choiceq0119

    2. True or false: True and False are Python Boolean values, and their first letters must be capitalized.

    1. ✅ True
    2. ❌ False
    Show answer

    Answer

    A. ✅ True

    Why: Python's Boolean literals are True and False with a capital first letter. Writing true or false creates ordinary names and raises NameError.

  3. Multiple choiceq0126

    3. 🧋 What is 'hello world'.split()?

    1. ['hello','world']
    2. ['helloworld']
    3. ['hello world']
    4. ['hello',' ','world']
    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. Multiple choiceq0150

    4. What happens with s = "abc"; s[0] = "A"?

    1. It becomes Abc
    2. It raises TypeError
    3. It automatically becomes a list
    4. It writes only once
    Show answer

    Answer

    B. It raises TypeError

    Why: Strings are immutable, so item assignment is rejected with TypeError. Build a new string with slicing, replace() or a join instead.

  5. Fill in the blankq0110

    5. Ming formats a milk tea price with f"{3.14159:.2f}" yuan. What is the output?

    Show answer

    Answer

    3.14

    Why: :.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. Fill in the blankq0127

    6. 🧋 What type does '1,2,3'.split(',') return?

    Show answer

    Answer

    list

    Why: split() always returns a list, even when there is only one field. The elements are strings, so numeric fields still need conversion.

  7. Fill in the blankq0185

    7. Replace the hyphens in an order with spaces. Which method completes the blank?

    order = 'latte-large'
    print(order.____('-', ' '))
    Show answer

    Answer

    replace

    Why: replace('-', ' ') returns a new string with every hyphen swapped for a space. The original order string is unchanged because strings are immutable.

  8. Output predictionq0109

    8. Ming truncates the shop name. What does this code print?

    print("Python milk tea"[:3])
    Show answer

    Answer

    Pyt

    Why: [: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. Output predictionq0144

    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. Output predictionq0168

    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

    False

    Why: 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.

Open full practice