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 bool()
Python Matrices and NumPy Arrays
Python Program to Add Two Matrices
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Trim Whitespace From a String
Python map()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Find HCF or GCD
Python range()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Set copy()
Python Program to Convert Decimal to Binary Using Recursion
Deep Learning with Python - Francois Cholletf
Python Program to Merge Two Dictionaries
Python List clear()
Python delattr()
Python list()
Python Set difference_update()
Python Program to Delete an Element From a Dictionary
Python Program to Split a List Into Evenly Sized Chunks
Python String isalpha()
Python String rstrip()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Find LCM
Python Program to Find Sum of Natural Numbers Using Recursion
Python Set remove()
Python Program to Reverse a Number
Python List index()
Python Program to Check Whether a String is Palindrome or Not
Python Type Conversion and Type Casting
Python Dictionary copy()