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 Dictionary fromkeys()
Python String rstrip()
Python Anonymous / Lambda Function
Python Exception Handling Using try, except and finally statement
Python next()
Python frozenset()
Python pow()
Python Program to Make a Simple Calculator
Python Program to Print all Prime Numbers in an Interval
Python Program to Make a Flattened List from Nested List
Python Program to Solve Quadratic Equation
Python Set symmetric_difference_update()
Python Program to Display Powers of 2 Using Anonymous Function
Python strftime()
Python Program to Trim Whitespace From a String
Python String join()
Python delattr()
Python while Loop
Python zip()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Decorators
Python del Statement
Python Program to Check Whether a String is Palindrome or Not
Python String index()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Operator Overloading
Python Custom Exceptions
Python Global Keyword
Python Program to Randomly Select an Element From the List
Python Program to Find Hash of File
Python String split()
Python Program to Illustrate Different Set Operations