Editor

Python string MCQ questions with answers

Indexing, slicing, methods and formatting: predict the output, then check.

0 of 20 answered

  1. Question 1 · Easy

    What is the output of this code?

    s = "PYTHON"
    print(s[0], s[-1], s[2])
    Show the answer

    Answer: B

    Indexes start at 0, so s[0] is P and s[2] is T. Negative indexes count from the end, and s[-1] is the last character, N.

    Run this code in the editor
  2. Question 2 · Easy

    What is the output of this code?

    s = "Programming"
    print(s[3:7])
    Show the answer

    Answer: C

    A slice s[start:stop] includes the start index and stops before the stop index, so s[3:7] takes indexes 3, 4, 5 and 6: g, r, a, m.

    Run this code in the editor
  3. Question 3 · Easy

    What is the output of this code?

    s = "computer"
    print(s[-3:], s[:-3])
    Show the answer

    Answer: A

    s[-3:] starts three characters from the end and runs to the end, giving ter. s[:-3] runs from the start up to, but not including, that same position, giving compu.

    Run this code in the editor
  4. Question 4 · Medium

    What is the output of this code?

    s = "abcdefgh"
    print(s[::2], s[1::3])
    Show the answer

    Answer: D

    The third number in a slice is the step. s[::2] takes every second character from index 0 (aceg), and s[1::3] takes indexes 1, 4 and 7 (beh).

    Run this code in the editor
  5. Question 5 · Hard

    What is the output of this code?

    s = "PYTHON"
    print(s[-2:-5:-1])
    Show the answer

    Answer: C

    With a step of -1 the slice walks backwards from s[-2] (O) and stops before s[-5] (Y), so it takes O, H, T. The stop index is never included, and s[-1] is not the start here.

    Run this code in the editor
  6. Question 6 · Medium

    What is the output of this code?

    name = "python"
    name.upper()
    print(name)
    Show the answer

    Answer: D

    Strings are immutable, so upper() returns a new string instead of changing name. The result is thrown away because it is not assigned; name = name.upper() would keep it.

    Run this code in the editor
  7. Question 7 · Hard

    What is the output of this code?

    line = "red  green"
    print(line.split())
    print(line.split(" "))
    Show the answer

    Answer: B

    With no argument, split() splits on any run of whitespace, so the double space acts as one separator. split(" ") splits at every single space, so the two spaces leave an empty string between them.

    Run this code in the editor
  8. Question 8 · Easy

    What is the output of this code?

    letters = ["a", "b", "c"]
    print("-".join(letters))
    print("".join(letters).upper())
    Show the answer

    Answer: A

    "-".join(letters) puts the separator only between the items, not at the ends. "".join(letters) gives abc, and upper() turns it into ABC.

    Run this code in the editor
  9. Question 9 · Medium

    What is the output of this code?

    s = "banana"
    print(s.find("an"), s.count("an"), s.find("x"))
    Show the answer

    Answer: D

    find() returns the index of the first match, and an first appears at index 1. count() counts non-overlapping matches (2). When there is no match, find() returns -1 rather than raising an error.

    Run this code in the editor
  10. Question 10 · Medium

    What is the output of this code?

    s = "a-b-c-d"
    print(s.replace("-", "+", 2))
    print(s)
    Show the answer

    Answer: C

    The third argument of replace() limits how many replacements are made, starting from the left, so only the first two - change. replace() returns a new string, so s itself is unchanged.

    Run this code in the editor
  11. Question 11 · Hard

    What is the output of this code?

    print("apple" < "Banana", "10" < "9")
    Show the answer

    Answer: A

    Strings compare character by character using Unicode code points. Uppercase letters come before lowercase ones (B is 66, a is 97), so "apple" < "Banana" is False. "10" < "9" compares 1 with 9 first, so it is True.

    Run this code in the editor
  12. Question 12 · Medium

    What is the output of this code?

    raw = "  ab-2024 "
    code = raw.strip().upper()
    print(code)
    print(code.startswith("AB"), "2024" in code, code.isdigit())
    Show the answer

    Answer: D

    strip() removes the outer spaces and upper() gives AB-2024. It starts with AB and contains 2024, but isdigit() is False because the string also has letters and a hyphen.

    Run this code in the editor
  13. Question 13 · Easy

    Which error does this code raise?

    word = "cat"
    word[0] = "b"
    print(word)
    Show the answer

    Answer: B, TypeError: 'str' object does not support item assignment

    Strings are immutable, so you cannot assign to one of their characters. Build a new string instead, for example word = "b" + word[1:].

    Run this code in the editor
  14. Question 14 · Medium

    Which error does this code raise?

    numbers = [1, 2, 3]
    print("-".join(numbers))
    Show the answer

    Answer: A, TypeError: sequence item 0: expected str instance, int found

    join() accepts only strings, and it does not convert numbers for you. Convert the items first: "-".join(str(n) for n in [1, 2, 3]) gives 1-2-3.

    Run this code in the editor
  15. Question 15 · Medium

    Which error does this code raise?

    s = "python"
    print(s[2:10])
    print(s[10])
    Show the answer

    Answer: C, IndexError: string index out of range

    A slice never raises an error: s[2:10] stops at the end of the string and prints thon. A single index must exist, so s[10] raises IndexError.

    Run this code in the editor
  16. Question 16 · Easy

    How many characters does the string "a\nb" contain?

    Show the answer

    Answer: C, 3

    The escape sequence \n is written with two symbols but stands for one character, a newline. So the string holds a, a newline and b: three characters.

  17. Question 17 · Easy

    What is the difference between s.find(x) and s.index(x) when x is not in s?

    Show the answer

    Answer: A, find() returns -1 and index() raises ValueError

    Both methods return the position of the first match. When there is no match, find() returns -1, while index() raises ValueError: substring not found.

  18. Question 18 · Easy

    For a non-empty string s, which index refers to its last character?

    Show the answer

    Answer: D, len(s) - 1

    Indexes run from 0 to len(s) - 1, and -1 is a shortcut for that last position. s[len(s)] raises IndexError, and -0 is the same as 0, the first character.

  19. Question 19 · Medium

    Which expression gives the string s reversed?

    Show the answer

    Answer: B

    A slice with step -1 walks the whole string backwards and returns a new string. Strings have no reverse() method, reversed(s) returns an iterator rather than a string, and s[-1:0:-1] stops before index 0, losing the first character.

  20. Question 20 · Hard

    Given value = 42.5, which line prints Total: 42.50, with the number shown to two decimals and right-aligned in 8 characters?

    Show the answer

    Answer: B

    In 8.2f, 8 is the minimum width and .2f gives two decimals; numbers are right-aligned by default, so 42.50 gets three spaces in front. < would left-align it, .8f shows eight decimals, and 8 alone keeps 42.5.

Keep going