Python Multi-Line Strings

Challenge: How to create a multi-line string in Python?

In this tutorial, I’ll show you four methods to create and maintain multi-line strings in Python. The most Pythonic ones are the first two methods with triple single quotes ''' ... ''' or triple double quotes """ ... """ that wrap a string across multiple lines.

Method 1: Multi-Line String with Triple Single Quotes

First, you can create a multi-line string in Python using triple single quotes:

# Multi-line string with triple single quotes
shakespeare = '''
Well, you have made a simple choice; you know not
how to choose a man: Romeo! no, not he; though his
face be better than any man's, yet his leg excels
all men's; and for a hand, and a foot, and a body,
though they be not to be talked on, yet they are
past compare: he is not the flower of courtesy,
but, I'll warrant him, as gentle as a lamb. Go thy
ways, wench; serve God. What, have you dined at home?
'''

print(type(shakespeare))
# <class 'str'>

Method 2: Multi-Line String with Triple Double Quotes

Second, you can create a Python multi-line string using triple double quotes:

shakespeare = """
Well, you have made a simple choice; you know not
how to choose a man: Romeo! no, not he; though his
face be better than any man's, yet his leg excels
all men's; and for a hand, and a foot, and a body,
though they be not to be talked on, yet they are
past compare: he is not the flower of courtesy,
but, I'll warrant him, as gentle as a lamb. Go thy
ways, wench; serve God. What, have you dined at home?
"""

print(type(shakespeare))
# <class 'str'>

Method 3: Multi-Line String with New-Line Characters

Third, you can create a multi-line string using the newline character:

shakespeare = 'Well, ... not\nhow to choose a man'
print(shakespeare)

The output is the following multi-line string:

Well, ... not
how to choose a man

Method 4: Multi-Line String with String Concatenation

Fourth, you can use a combination of string concatenation, escaping newline characters with the single backslash \, and newline characters '\n' to obtain a multi-line string.

shakespeare = "Well, you have made a simple choice; you know not\n" \
              + "how to choose a man: Romeo! no, not he; though his\n" \
              + "face be better than any man's, yet his leg excels\n" \
              + "all men's; and for a hand, and a foot, and a body\n"
            
print(type(shakespeare))
# <class 'str'>

Note that the ‘+’ symbol is not strictly needed for string concatenation which leads us to Method 5:

Method 5: Multi-Line String with String Concatenation Without Plus +

Fifth, you can create a multi-line string by using the string concatenation without the + operator using the property that two adjacent strings in the same line are automatically concatenated in Python.

shakespeare = "Well, you have made a simple choice; you know not\n" \
              "how to choose a man: Romeo! no, not he; though his\n" \
              "face be better than any man's, yet his leg excels\n" \
              "all men's; and for a hand, and a foot, and a body\n"
            
print(type(shakespeare))
# <class 'str'>

Again, note the backslash before the newlines. This removes the special meaning of the newline character, so that Python doesn’t throw an error. If you’d skip escaping the newline character avoiding the use of the single trailing backslash in each line, Python cannot make sense out of it because the code becomes ambiguous:

Another way to remove this ambiguity is shown in Method 6:

Method 6: Multi-Line String with String Concatenation With Parentheses

Sixth, you can create a multi-line string by using string concatenation and wrap all lines within a parentheses environment ( ... ) to tell Python that the expression is not over yet when the line is finished.

shakespeare = ("Well, you have made a simple choice; you know not\n"
              "how to choose a man: Romeo! no, not he; though his\n"
              "face be better than any man's, yet his leg excels\n"
              "all men's; and for a hand, and a foot, and a body\n")
            
print(type(shakespeare))
# <class 'str'>

This is relatively easy to read and it doesn’t require the use of the + and the \ operators.

Test Your Understanding — Python Puzzle Multi-Line String

Consider this Python puzzle:

print("""
A
B
C
"""=="\nA\nB\nC\n")

Exercise: What is the output of this Python puzzle?

What is going on in this puzzle? The basic idea is to show two different ways of writing the same multi-line string literal in Python.

The first is the direct way to write a multi-line string in Python: As a string with multiple code lines enclosed by triple-quotes '''...''' or """...""".

The second is a more concise way to write the same string. We specify the line breaks with the new line character '\n'.

These two ways of breaking lines in Python strings are the basis for advanced features and code snippets. Understanding them will increase your coding skills a bit further.

Click to solve the puzzle on our interactive Python puzzle app—are you a master coder?
Test your skills now!

Leave a Comment