Triple-quote
- Python course
- Beginner
- Lesson 34/116
- 4 min
Learn multi-line text with """...""".
Triple-quote strings
Triple quotes (""" or ''') let you write multi-line strings:
message = """Line 1
Line 2
Line 3"""
print(message)This way you do not need \n; line breaks inside the string are preserved. It is handy for long messages and docstrings.
Docstring
A triple-quoted string on the first line of a function or class becomes a docstring. It explains what the function does:
def greet(name):
"""Greets the user."""
return f"Hello {name}"The docstring is shown with help(greet). We'll cover this in the functions group.
Practice Print a multi-line string
Assign a triple-quoted string to message: """Line 1
Line 2""". Then print print(message).
Starter code
# Multi-line stringSample solution
message = """Line 1
Line 2"""
print(message)Expected output
Line 1 Line 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What are triple quotes for?
- Multi-line strings Correct answer
- Writing numbers
- Writing comments
- Declaring variables
Explanation: Triple quotes are used for multi-line strings.
2 Triple-quoted string
message = ____"""Line 1
Line 2"""
message = """"""Line 1
Line 2"""
Explanation: """ starts a multi-line string.
3 Match the string type
| Term | Meaning |
|---|---|
""" |
Multi-line |
f" |
Embedding variables |
r" |
Raw string |
"" |
Normal string |