Table of contents
1.
Introduction
2.
What is a Python Multiline String?
2.1.
1. Multiline String in Python Using Triple-Quotes
2.2.
Python
2.3.
2. Multiline String in Python Using Parentheses and Single/Double Quotes
2.4.
Python
2.5.
3. Multiline String in Python Using Backslash
2.6.
Python
2.7.
4. Multiline String in Python Using Brackets
2.8.
Python
2.9.
5. Multiline String in Python Using join()
2.10.
Python
2.11.
6. Multiline String in Python Using string.format()
2.12.
Python
2.13.
7. Multiline String in Python Using %
2.14.
Python
3.
Example of Multiline String in Python
3.1.
Python
3.2.
Python
3.3.
Python
4.
Frequently Asked Questions
4.1.
How can we join multiline strings into a single line?
4.2.
How to create a multiline string in Python?
4.3.
Do multiline strings in Python preserve formatting such as line breaks?
4.4.
How do multiline strings handle whitespace at the beginning or end of the string?
5.
Conclusion
Last Updated: Oct 26, 2024
Easy

Multiline String in Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, handling strings is a fundamental aspect of programming, and one of the most versatile features is the ability to work with multiline strings. These strings allow developers to create text that spans multiple lines, making it easier to format large blocks of text, write documentation, or create more readable code. With the use of triple quotes—either single (''') or double (""")—Python enables seamless integration of multiline strings into your applications. This blog will explore the various ways to define, manipulate, and utilize multiline strings in Python, showcasing their benefits and providing practical examples for effective implementation.

Multiline String in Python

What is a Python Multiline String?

A multiline string in Python is a string that spans across multiple lines of code, created by enclosing text within triple quotes (""" or '''). When you create a multiline string, all line breaks, indentation, and whitespace within the triple quotes are preserved in the string.

As we all know, strings are used to store words and sentences in any programming language. There often arises the need to create multiline strings and store them as different lines. Python multiline strings come in handy in this case by providing the developer with the opportunity to do the same. Python multiline string can be used by declaring the sentence in triple quotes.

Below are some approaches for a better understanding of Python multiline strings:

  1. Using Triple-Quotes
  2. Using parentheses and single/double quotes
  3. Using Backslash
  4. Using Brackets
  5. Using join()
  6. Using string.format()
  7. Using %

1. Multiline String in Python Using Triple-Quotes

Multiline strings in Python can be created using triple quotes. This allows for easy inclusion of line breaks and formatting.

  • Python

Python

multiline_string = """This is a multiline string.
It spans multiple lines.
Easily readable!"""
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

 

Output:

This is a multiline string.
It spans multiple lines.
Easily readable!

2. Multiline String in Python Using Parentheses and Single/Double Quotes

You can create multiline strings using parentheses combined with single or double quotes, allowing implicit concatenation.

  • Python

Python

multiline_string = (
'This is a multiline string. '
'It uses parentheses to extend over multiple lines.'
)
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

Output:

This is a multiline string. It uses parentheses to extend over multiple lines.

3. Multiline String in Python Using Backslash

Backslashes at the end of a line allow string continuation on the next line, preserving the line breaks.

  • Python

Python

multiline_string = "This is a multiline string, \
spanning multiple lines using backslashes."
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

 

Output:

This is a multiline string, spanning multiple lines using backslashes.

4. Multiline String in Python Using Brackets

Using brackets, such as square brackets or curly braces, can help create multiline strings by breaking them into a list of strings.

  • Python

Python

multiline_string = [
"This is a multiline string,",
"created using brackets.",
"Each element represents a line."
]
print('\n'.join(multiline_string))
You can also try this code with Online Python Compiler
Run Code

 

Output:

This is a multiline string,
created using brackets.
Each element represents a line.

5. Multiline String in Python Using join()

The join() method can combine elements of a list or tuple into a single string, allowing for multiline output.

  • Python

Python

lines = ["This is a multiline string.", "Using join() to combine lines.", "Each line is separate."]
multiline_string = '\n'.join(lines)
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

Output:

This is a multiline string.
Using join() to combine lines.
Each line is separate.

6. Multiline String in Python Using string.format()

The string.format() method can insert formatted data into multiline strings, improving readability.

  • Python

Python

name = "Mohan"
multiline_string = """Hello, {0}.
Welcome to the world of Python!
Enjoy your coding journey.""".format(name)
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Hello, Mohan.
Welcome to the world of Python!
Enjoy your coding journey.

7. Multiline String in Python Using %

Using the % operator for string formatting allows you to embed variables within a multiline string.

  • Python

Python

name = "Rohit"
multiline_string = """Hello, %s.
Welcome to the world of Python!
Happy coding!""" % name
print(multiline_string)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Hello, Rohit.
Welcome to the world of Python!
Happy coding!

Example of Multiline String in Python

In this section, we will look at an example of Python multiline strings. 

  • Python

Python

multiLineStr = """This is an example of Python multiline string
This is Line Number 2
This is Line Number 3 """

print(multiLineStr, end ="\n")
You can also try this code with Online Python Compiler
Run Code

Output

This is an example of Python multiline string
This is Line Number 2
This is Line Number 3 

We can also use single quotes instead of double quotes to declare the Python multiline string. 

  • Python

Python

multiLineStr = '''This is an example of Python multiline string
This is Line Number 2
This is Line Number 3 '''

print(multiLineStr, end ="\n")
You can also try this code with Online Python Compiler
Run Code

Output

This is an example of Python multiline string
This is Line Number 2
This is Line Number 3 

As you can see, using double quotes and single quotes produces the same result. 

We can also use \n to declare a Python multiline string. Let us look at an example of that, 

  • Python

Python

multiLineStr = ("This is an example of Python multiline string \n" 
"This is Line Number 2 \n"
"This is Line Number 3")

print(multiLineStr, end ="\n")
You can also try this code with Online Python Compiler
Run Code

Output

This is an example of Python multiline string
This is Line Number 2 
This is Line Number 3

Now let's discuss some frequently asked questions associated with a Python multiline string.

Frequently Asked Questions

How can we join multiline strings into a single line?

We can use the .join() method present in Python to join multiple strings into a single line. 

How to create a multiline string in Python?

In Python, you can create a multiline string using triple quotes (''' or """). This allows you to include line breaks and preserve the text formatting easily without additional characters for new lines.

Do multiline strings in Python preserve formatting such as line breaks?

Yes, multiline strings in Python preserve formatting, including line breaks and spaces. When printed or stored, the text appears exactly as defined, making it ideal for maintaining structure in larger blocks of text.

How do multiline strings handle whitespace at the beginning or end of the string?

Multiline strings in Python retain any whitespace at the beginning or end of each line. This means that leading spaces and trailing spaces will be included in the final output, which can affect formatting and alignment.

Conclusion

In this blog, we have discussed multiline strings in Python. Multiline strings in Python offer a flexible and efficient way to handle large blocks of text while maintaining readability and formatting. Whether using triple quotes, parentheses, backslashes, or string formatting methods, developers can easily create and manipulate multiline strings to suit their needs. Understanding the various techniques for implementing multiline strings enhances code clarity and allows for better organization of textual data.

Do check out our blogs on object-oriented programming and data structures

Recommended Readings:

Recommended problems -

Also, try Code360 for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass