Practice: Multiline Strings

« Read the chapter

Q1. Three-line message

BeginnerCoding

Create a multiline string holding the lines one, two and three and print it.

Goal: Create multiline strings.

Q2. Count the lines

BeginnerCoding

Print how many lines the text """a\nb\nc\nd""" has by using splitlines (write it with triple quotes and real line breaks).

Goal: Count lines.

Q3. Quotes inside

BeginnerCoding

Print a triple-quoted string that contains both 'single' and "double" quote marks on one line.

Goal: Use quotes inside triple quotes.

Q4. Skip first newline

IntermediateCoding

Write a string that starts on a new line in the code but does not begin with a new line; it should contain hello then a newline then world. Print its repr.

Goal: Control the start of a string.

Q5. Join long text

IntermediateCoding

Using parentheses and adjacent literals, print the single line: Learn Python step by step.

Goal: Join adjacent literals.

Q6. Number the lines

IntermediateCoding

Print each line of the text with its number starting at 1, for the lines red, green and blue.

Goal: Process lines.

Q7. Dedent text

AdvancedCoding

Use textwrap.dedent to print the text below with no leading spaces: two lines indented by four spaces, hello and world.

Goal: Use dedent.

Q8. Template letter

IntermediateCoding

Given name = "Ravi" print two lines: Hello Ravi, and Welcome! using a triple-quoted f-string.

Goal: Use f-strings with triple quotes.

Q9. Build from list

IntermediateCoding

Join the words in ["x", "y", "z"] into a three line string and print it.

Goal: Join lines.

Q10. Hidden newline

BeginnerOutput

What does this print?

Code
s = """
hi
"""
print(len(s))

Goal: Count hidden characters.

Q11. Lines count

BeginnerOutput

What does this print?

Code
s = """a
b
c"""
print(s.count("\n"), len(s.splitlines()))

Goal: Count line breaks.

Q12. Repr shows all

IntermediateOutput

What does this print?

Code
s = """x
  y"""
print(repr(s))

Goal: Read repr.

Q13. Adjacent pieces

IntermediateOutput

What does this print?

Code
t = ("ab"
     "cd" "ef")
print(t, len(t))

Goal: Predict adjacent literals.

Q14. Backslash continuation

IntermediateOutput

What does this print?

Code
s = """one \
two"""
print(s)

Goal: Read continuation.

Q15. Docstring access

AdvancedOutput

What does this print?

Code
def f():
    """Say hi."""
    return 1
print(f.__doc__)

Goal: Know docstrings.

Q16. Unclosed string

BeginnerDebugging

This code has a SyntaxError. Fix it so it prints two lines.

Code
print("""line one
line two")

Goal: Close strings correctly.

Q17. Extra indent

IntermediateDebugging

The output has four unwanted spaces on each line. Fix the code so it prints hello and world with no spaces at the left.

Code
def show():
    text = """
    hello
    world
    """
    print(text.strip())
show()

Goal: Remove indentation.

Q18. Missing space

BeginnerDebugging

This prints Hello,world. It should print Hello, world. Fix it.

Code
print("Hello,"
      "world")

Goal: Fix joined literals.

Q19. Why dedent

IntermediateConceptual

Explain why textwrap.dedent is useful for multiline strings in functions.

Goal: Explain dedent.

Q20. Triple vs plus

IntermediateConceptual

Explain the difference between a triple-quoted string and parentheses with adjacent literals.

Goal: Choose a string style.