Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    14Topics
    |
    15 Quizzes
  2. Python: Control Flow
    12Topics
    |
    12 Quizzes
  3. Python: Errors
    5Topics
    |
    5 Quizzes
Lesson 1, Topic 13
In Progress

Python: Multi-line Strings

Yasin Cakal 5 Oct 2021
Lesson Progress
0% Complete

Python strings are incredibly flexible, but when we attempt to create a string that spans multiple lines, we encounter a SyntaxError. Python provides a solution in the form of multi-line strings. By using three quote marks (“”” or “‘) rather than one, we indicate to the program that the string does not end until the following triple-quote. This method is useful when the string being defined contains a large number of quotation marks, and we want to avoid prematurely closing it. Here is an example:

shopping_list = """
1.) Tomatoes
2.) Onions
3.) Chicken
"""
print(shopping_list)

# Previously this would require multiple print functions.
print("1.) Tomatoes")
print("<meta charset="utf-8">2.) Onions")
print("<meta charset="utf-8">3.) Chicken")

Output:

1.) Tomatoes
2.) Onions
3.) Chicken

1.) Tomatoes
2.) Onions
3.) Chicken

As you can see, we can print the shopping list with code that is much more concise.

%d bloggers like this: