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:
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String translate()
Python Shallow Copy and Deep Copy
Python list()
Python sorted()
Python String swapcase()
Python enumerate()
Python Program to Check Whether a String is Palindrome or Not
Python String isalpha()
Python String split()
Python Program to Get the Full Path of the Current Working Directory
Python Program to Check the File Size
Python Set symmetric_difference_update()
Python zip()
Python String maketrans()
Python Program to Display Fibonacci Sequence Using Recursion
Python Deep Learning Cookbook - Indra den Bakker
Python Statement, Indentation and Comments
Python Strings
Python strptime()
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Create a Countdown Timer
Python Set intersection_update()
Python String format()
Python Program to Check if a Key is Already Present in a Dictionary
Python Namespace and Scope
Python Dictionary fromkeys()
Python Object Oriented Programming
Python Program to Slice Lists
Python Global, Local and Nonlocal variables
Python List reverse()
Python Program to Count the Occurrence of an Item in a List