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 Machine Learning - Sebastian Raschka
Python range()
Python String splitlines()
Python Program to Get the Full Path of the Current Working Directory
Python Exception Handling Using try, except and finally statement
Python List clear()
Python hash()
Python str()
Python Program to Shuffle Deck of Cards
Python Set symmetric_difference()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String casefold()
Python Global Keyword
Python Get Current time
Python filter()
Intelligent Projects Using Python - Santanu Pattanayak
Python format()
Python int()
Python Program to Represent enum
Python bool()
Python RegEx
Python set()
Python String isalnum()
Python Iterators
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python enumerate()
Python Program to Get the Class Name of an Instance
Python Matrices and NumPy Arrays
Python Set isdisjoint()
Python Shallow Copy and Deep Copy
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Count the Number of Occurrence of a Character in String