TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Python Basic

Python Multiline String

Last updated: Apr 18, 2025 4:27 pm
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
1 month ago
Share
6 Min Read
SHARE

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.

Contents
Create Multiline String in PythonPython multiline string with triple quotesPython multiline string using parenthesesCreate a multiline string using backslashesCreate a multiline string with Python join()Python multiline string with variablesCreate Python multiline string with formatFormat Python multiline string using %Python multiline string with textwrap.dedent()Create Python multiline string from stringioSummary: Python Multiline String

Create Multiline String in Python

Let’s explore the most common and new ways to manage multiline strings in Python.

Python multiline string with examples

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.

mulitline string in triple quotes

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.

python mulitline string in brackets

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.

mulitline string using backslash

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.

python mulitline string using join method

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 and subscribe to TechBeamers YouTube channel for more engaging stuff.

Enjoy Coding!

Related

Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • Python List ComprehensionSep 10
  • Python XOR Operator (^) Explained with Simple Examples for BeginnersAug 5
  • Python Float NumbersSep 28
  • Python Check Variable TypeOct 28
  • Python Nested ListOct 29
  • Python Exception HandlingAug 13
  • Python: 30 Programming Tips & TricksSep 18
  • Python Try-ExceptJun 16
  • Python Keywords, Identifiers, & VariablesJul 15
  • Python Statement & IndentationOct 30
View all →

RELATED TUTORIALS

Check Python Version Using Code

Python Check Version

By Harsh S.
1 month ago
Unique List in Python

How to Get Unique Values from a List

By Meenakshi Agarwal
1 month ago
Python String indexOf Using Find() and Index()

How to Use String indexOf in Python

By Meenakshi Agarwal
1 month ago
7 Ways to Remove Whitespace from a String in Python

How to Remove Whitespace from a String

By Meenakshi Agarwal
1 month ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz