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 Display Fibonacci Sequence Using Recursion
Python Program to Reverse a Number
Python Program to Get a Substring of a String
Python hex()
Python ascii()
Python repr()
Python classmethod()
Python Program to Create a Countdown Timer
Python String translate()
Python String zfill()
Python dir()
Python String endswith()
Python id()
Python Program to Randomly Select an Element From the List
Python pass statement
Python Program to Merge Mails
Python Program to Sort Words in Alphabetic Order
Python Dictionary pop()
Python sorted()
Python oct()
Python Tuple count()
Python Dictionary get()
Python Program to Generate a Random Number
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String replace()
Python Program to Check if a Number is Positive, Negative or 0
Python Set issubset()
Python Custom Exceptions
Python Program to Capitalize the First Character of a String
Python Program to Find the Largest Among Three Numbers
Python Program to Sort a Dictionary by Value
Python Program to Convert Kilometers to Miles