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 open()
Python Program to Find the Factors of a Number
Python break and continue
Python Custom Exceptions
Python String encode()
Python Program to Get the File Name From the File Path
Python Program to Find Hash of File
Python strftime()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String casefold()
Python float()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Check Whether a String is Palindrome or Not
Python min()
Python String splitlines()
Python Program to Check Prime Number
Python reversed()
Python Machine Learning - Sebastian Raschka
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Machine Learning with Python for everyone - Mark E.Fenner
Python strptime()
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Copy a File
Python type()
Python staticmethod()
Python String maketrans()
Python Program to Access Index of a List Using for Loop
Python iter()
Python String istitle()
Python compile()
Python isinstance()
Python sorted()