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 Check if a Key is Already Present in a Dictionary
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String encode()
Python Program to Check if a Number is Odd or Even
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python List count()
Python String istitle()
Python Program to Check If Two Strings are Anagram
Python Program to Get Line Count of a File
Python File I/O Operation
Python Statement, Indentation and Comments
Python String rindex()
Python List clear()
Python Program to Print Colored Text to the Terminal
Python Tuple index()
Python String index()
Python Program to Convert Kilometers to Miles
Python Program to Get the Full Path of the Current Working Directory
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Set isdisjoint()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Multiple Inheritance
Python Program to Transpose a Matrix
Python Closures
Python List copy()
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Illustrate Different Set Operations
Python Set discard()
Python list()
Python String strip()
Python String replace()
Python Program to Check Whether a String is Palindrome or Not