Multiline String in Python
Last Updated :
24 Jul, 2024
A sequence of characters is called a string. In Python, a string is a derived immutable data type—once defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.
Python has multiple methods for defining strings. Single quotations (''), double quotes (" "), and triple quotes (''' ''') are all acceptable.
Python Multiline String
There are several approaches to implementing the multiline string in Python. To define multi-line strings, we can use backlash, brackets, and triple quotes. To better understand the Python multiline string, below are the following approaches:
- Using Triple-Quotes
- Using parentheses and single/double quotes
- Using Backslash
- Using Brackets
- Using join()
- Using string.format()
- Using %
Python Multiline String Using Triple-Quotes
Using the triple quotes style is one of the easiest and most common ways to split a large string into a multiline Python string. Triple quotes (''' or """) can be used to create a multiline string. It allows you to format text over many lines and include line breaks. Put two triple quotes around the multiline Python string, one at the start and one at the end, to define it.
Python
multiline_string = '''This is a
multiline
string.'''
print(multiline_string)
OutputThis is a
multiline
string.
Create a Python Multiline String Using parentheses and single/double quotes
A different method to define a multiline string in Python is to include its components in brackets. Both single quotes ('') and double quotations ("") are acceptable, as was previously shown. To learn how to use them, look at the examples provided below.
Python
colors = ("multi-line string"
"red \n"
"blue \n"
"green \n"
"yellow \n"
)
print(colors)
Outputmulti-line stringred
blue
green
yellow
Python Multiline String Using Backslash
In Python, we can divide a string into many lines by using backslashes. The backslash character in Python serves as a line continuation character. It is used to combine text that consists of individual lines.
Python
x = "multiline String" \
"I love Python" \
"Python Langauge"
print(x)
Outputmultiline StringI love PythonPython Langauge
Create a Python Multiline String Using Brackets
When there is no newline in the string, there is another method for declaring a multiline string in Python that involves using brackets. Let's examine it in the following example:
Python
x = "multiline String" \
"I love Python" \
"Python Langauge"
print(x)
Outputmultiline StringI love PythonPython Langauge
Python Multiline String Creation Using join()
In this option, we break up multiple strings for printing multiline strings using Python's string join() function. Because it would be difficult to skip a space when using brackets or backslashes in a very lengthy string, the two alternatives above included a condition to check for spaces when we use them. However, this issue may be fixed by utilizing the string join() method. Below is an example to help you understand it.
Python
x = ' '.join(("multiline String ",
"Python Language",
"Welcome to GFG"))
print(x)
Outputmultiline String Python Language Welcome to GFG
Multiline String in Python Using string.format()
In this option, we can use variables to format a string by using string.format(). This can be helpful for writing scripts, code, or other formatted text. Below is an example to help you understand it.
Python
car = "Ferrari"
price = 250000
x = "Hello, The price of {} is {}".format(car, price)
print(x)
OutputHello, The price of Ferrari is 250000
Multiline String Creation Using % in Python
In this option, Python's % operator is used to format strings. It returns a formatted string after receiving as inputs a tuple of values and a format string. The values in the tuple are represented as substituted in the format string.
Python
name = "Rahul"
points = 100
x = "Hello, %s! You have %d coins." % (name, points)
print(x)
OutputHello, Rahul! You have 100 coins.
The % operator can also be used to format numbers, dates, and times.
Python
marks = 49.99
total_marks = "%0.2f" % marks
print(total_marks)
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read