Table of Contents
In this example, you will learn to create a long multiline string.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using triple quotes
my_string = '''The only way to learn to program is by writing code.''' print(my_string)
Output
The only way to learn to program is by writing code.
You can use '''(multiline string)''' or """(multiline string)""" to print a multiline string as shown above.
2. Example 2: Using parentheses and a single/double quotes
my_string = ("The only way to \n"
"learn to program is \n"
"by writing code.")
print(my_string)
Output
The only way to learn to program is by writing code.
If you use (" ") syntax, you need to specify the newlines explicitly using \n.
3. Example 3: Using \
my_string = "The only way to \n" \
"learn to program is \n" \
"by writing code."
print(my_string)
Output
The only way to learn to program is by writing code.
You can use \ as in the above example code to write a multiline string.
Related posts:
Python Program to Iterate Through Two Lists in Parallel
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Print Hello world!
Python Variables, Constants and Literals
Python globals()
Python String rfind()
Python Program to Print Output Without a Newline
Python Program to Find the Largest Among Three Numbers
Python Dictionary get()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python chr()
Python classmethod()
Python Program to Create Pyramid Patterns
Python Decorators
Python eval()
Python Program to Append to a File
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python List copy()
Python Matrices and NumPy Arrays
Python Type Conversion and Type Casting
Python Program to Check If a String Is a Number (Float)
Python String maketrans()
Python Dictionary setdefault()
Python Generators
Python Program to Return Multiple Values From a Function
Python enumerate()
Python Program to Capitalize the First Character of a String
Python Set update()
Python String zfill()
Python Program to Get File Creation and Modification Date
Python Set copy()
Python String format()