liveBook · Manning

Multiline string

As explained in Get Programming: Learn to code with Python

A multiline string is a string literal or value whose text or source representation spans multiple lines.

A multiline string is a string value written across several lines. Triple quotes mark its beginning and end, and every character inside—including line breaks—becomes part of the string. section 19.2

A word list stored as one string

words = """art hue ink ... crosshatching """ The value keeps each word and its newline. A loop can inspect each character; when it finds "\n", it knows one word has ended and can extract that word. section 19.2

Triple-quoted strings support two related uses:

  • Store readable data spanning many lines, such as the complete list of valid words in a word game. section 19.2
  • Document a function with a docstring that describes parameters, behavior, return values, and types. section 21.3
Listing 19.1. Scrabble: Art Edition code for initializations
words = """art                      #1
hue
ink
...
crosshatching
"""
tiles = "hijklmnop"

all_valid_words = ()                #2
start = 0                           #3
end = 0                             #4
found_words = ()                    #5

Inspect the initialization code to see triple quotes surrounding a multiline word string. section 19.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains delimiters and embedded line breaks, then shows newline-based parsing and docstring structure.
Examples
Shows an art-word list, newline scanning, and a function docstring as concrete multiline-string examples.
What it assumes
Assumes familiarity with strings, variables, functions, and basic iteration.
What you can do after
Apply triple quotes to store multiline data or document a function with a readable docstring.
How it connects
Connects multiline strings with string indexing, newline characters, tuples, functions, parameters, and abstraction.

Also explained in

Compare how 3 books explain multiline string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.