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 Decorators
Python Program to Sort Words in Alphabetic Order
Python Program to Differentiate Between type() and isinstance()
Python Program to Add Two Matrices
Python id()
Python Dictionary popitem()
Python List
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python if...else Statement
Python Program to Display Powers of 2 Using Anonymous Function
Python Dictionary
Python bytearray()
Python String isnumeric()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Set update()
Python Errors and Built-in Exceptions
Python Program to Check if a Key is Already Present in a Dictionary
Python del Statement
Python len()
Python String find()
Python List count()
Python abs()
Python Multiple Inheritance
Python @property decorator
Python Set symmetric_difference()
Python Machine Learning - Sebastian Raschka
Python Set pop()
Python Data Types
Python tuple()
Python Program to Remove Punctuations From a String
Python Program to Convert Celsius To Fahrenheit
Python String format()