A Python multiline string is a string that allows text to span multiple lines. This tutorial covers several ways to create them. You might use them for docstrings, formatting SQL queries, or storing JSON data.
Create Multiline String in Python
Let’s explore the most common and new ways to manage multiline strings in Python.

Python multiline string with triple quotes
Triple quotes ("""
or '''
) are the most straightforward way to define multiline strings in Python. This method preserves the format, including line breaks and indentation.

Example: Multi-line string in Python
multiline_str = """This is a multiline string.
It spans multiple lines and retains formatting.
Perfect for large blocks of text."""
print(multiline_str)
Advantages:
- Retains line breaks and spaces.
- Ideal for docstrings and large text blocks.
Similarly, learn to create multiline comments in Python.
Python multiline string using parentheses
Parentheses allow you to split a string into multiple lines without adding newline characters. This method is recommended by PEP 8 for maintaining readability.

Example:
multiline_str = (
"This string spans multiple lines "
"without using explicit newline characters. "
"It keeps the string continuous and clean."
)
print(multiline_str)
Advantages:
- Avoids explicit newline characters.
- Recommended for readability.
Create a multiline string using backslashes
Backslashes (\
) can join strings across lines, although this method is less commonly used due to its potential for confusion.

Example:
multiline_str = "This is a multiline string " \
"joined with backslashes. " \
"It continues across multiple lines."
print(multiline_str)
Advantages:
- Useful for simple cases.
- Keeps the string on a single line logically.
Create a multiline string with Python join()
Python join() method is flexible for creating multiline strings from lists of strings in Python. You can customize separators, including newlines.

Example:
lines = [
"This is a multiline string.",
"Created using the join() method.",
"Each line is combined into one string."
]
multiline_str = '\n'.join(lines)
print(multiline_str)
Advantages:
- Allows precise control over separators.
- Useful for constructing strings from lists.
Python multiline string with variables
F-strings (formatted string literals) are available from Python 3.6 and later. They provide an elegant way to pass variables in multiline strings.
Example:
name = "Meenakshi"
age = 30
multiline_str = f"""
Hello, {name}!
You are {age} years old.
"""
print(multiline_str)
Advantages:
- Supports dynamic content insertion.
- Modern and concise formatting method.
Create Python multiline string with format
The string format() method is a simple way to insert variables into multiline strings. This method is compatible with Python versions before f-strings. Check from the following code using it for multiline string.
Example:
name = "Soumya"
age = 30
multiline_str = """
Hello, {}!
You are {} years old.
""".format(name, age)
print(multiline_str)
Advantages:
- Versatile and widely supported.
- Allows dynamic content with placeholders.
Format Python multiline string using %
The “%” operator is an older method for string formatting in Python. It is still useful for maintaining compatibility with legacy code. It can be tweaked to create multiline strings in Python.
Example:
name = "Harsh"
age = 30
multiline_str = """
Hello, %s!
You are %d years old.
""" % (name, age)
print(multiline_str)
Advantages:
- Useful for compatibility with older Python code.
- Simple formatting.
Python multiline string with textwrap.dedent()
The dedent() function from the textwrap module removes common leading whitespace from multiline strings in Python. It is useful for maintaining clean code formatting.
Example:
import textwrap
multiline_str = textwrap.dedent("""\
This is a multiline string.
Leading whitespace is removed.
This method is useful for maintaining clean code formatting.
""")
print(multiline_str)
Advantages:
- Removes common leading whitespace.
- Keeps the code clean and properly indented.
Create Python multiline string from stringio
StringIO from the io module can also be an effective way to create multiline strings in Python. It is more practical to use it when constructing large texts programmatically.
Example:
from io import StringIO
buffer = StringIO()
buffer.write("This is a multiline string.\n")
buffer.write("Constructed using StringIO.\n")
buffer.write("It can handle large texts efficiently.")
multiline_str = buffer.getvalue()
print(multiline_str)
Advantages:
- Efficient for large text constructions.
- Provides a file-like interface for strings.
Summary: Python Multiline String
Today, you learned many old and new ways to create multiline strings in Python. Practice with the examples we provided to excel in these techniques.
If you need to practice more, refer to our 40 Python exercises for beginners. With these exercises, you can span your learning to different areas of Python.
Lastly, our site needs your support to remain free. So, share this Python tutorial on social media (Linkedin/Twitter) if you gained some knowledge from reading it.
Enjoy Coding,
TechBeamers.