Python Practice Test: Strings and Files

  • String Methods and Reading Files
  • 10 questions
  • 13 minutes

Ten questions on text handling and reading and writing files. Tip: remember that string methods return new strings instead of changing the original.

Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.

Advertisement
Advertisement
  1. Question 1

    s = "Python"
    print(s[1:4])

    What does the code print?

  2. Question 2

    print("a,b,c".split(","))

    What does the code print?

  3. Question 3

    name = "Ana"
    age = 30
    print(f"{name} is {age}")

    What does the code print?

  4. Question 4

    s = "cat"
    s[0] = "b"

    What happens when this code runs?

  5. Question 5

    print("-".join(["x", "y", "z"]))

    What does the code print?

  6. Question 6

    print("banana".replace("a", "o", 2))

    What does the code print?

  7. Question 7

    Which mode should you pass to open() to add new text to the end of an existing file without erasing what is already in it?

  8. Question 8

    Why do many programmers write file code inside a with open(...) as f: block?

  9. Question 9

    print("hello".find("z"))

    What does the code print?

  10. Question 10

    with open("data.txt") as f:
        lines = f.readlines()
    print(repr(lines[0]))

    The file data.txt contains two lines, red and blue. What does the code print?

Questions you leave blank are not counted as wrong.

Advertisement

More Python tests

All Python Programming tests
13:00

Do you want to finish the test?