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 Swap Two Variables
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Node.js vs Python for Backend Development
Python Variables, Constants and Literals
Python List
Python format()
Python Program to Find the Factorial of a Number
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
How to Get Started With Python?
Python sleep()
Python Dictionary update()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Set union()
Python Namespace and Scope
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Count the Occurrence of an Item in a List
Python Operators
Python Set difference()
Python Statement, Indentation and Comments
Python Set pop()
Python Errors and Built-in Exceptions
Python List Comprehension
Python List append()
Python Program to Get the Class Name of an Instance
Python Operator Overloading
Python memoryview()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Merge Mails
Python Program to Find Sum of Natural Numbers Using Recursion
Python while Loop
Python pow()
Python String swapcase()