How to Split a Long String into Multiple Lines in Python?

In this tutorial, I will explain how to split a long string into multiple lines in Python. As a software developer working on a report generation tool, I encountered a scenario where long strings needed to fit within fixed-width columns for better readability. Then, I researched methods to split long strings into multiple lines while maintaining word boundaries. I will share my findings in this article with examples.

Split a Long String into Multiple Lines in Python

Let us see some important methods to split a long string into multiple lines in Python.

Read How to Insert a Python Variable into a String?

1. Use Parentheses, Brackets, or Braces

In Python, you can freely break lines inside parentheses (), square brackets [], or curly braces {} without using a backslash. This rule allows you to split a long string into multiple lines seamlessly. Here’s an example:

long_string = (
    "John Doe\n"
    "123 Main Street\n"
    "New York, NY 10001\n"
    "United States"
)
print(long_string)

Output:

John Doe
123 Main Street
New York, NY 10001
United States

I have executed the above example code and added the screenshot below.

Split a Long String into Multiple Lines in Python

As you can see, the string is defined across multiple lines using parentheses, and Python automatically concatenates them into a single string.

Read How to Split a String by Index in Python?

2. Use Triple Quotes

Python supports multi-line strings by enclosing them in triple quotes, either """ or '''. This approach is particularly useful when dealing with long strings that span multiple lines. Here’s an example:

long_string = """
SELECT *
FROM customers
WHERE country = 'United States'
ORDER BY last_name ASC;
"""
print(long_string)

Output:

SELECT *
FROM customers
WHERE country = 'United States'
ORDER BY last_name ASC;

I have executed the above example code and added the screenshot below.

How to Split a Long String into Multiple Lines in Python

Triple quotes allow you to preserve the line breaks and indentation in the string, making it ideal for SQL queries or multi-line text.

Read Convert Binary to Decimal in Python

3. Use Backslashes

Another way to split a long Python string into multiple lines is by using backslashes \. By placing a backslash at the end of each line, you can indicate that the string continues on the next line. Here’s an example:

long_string = "Emily Johnson\n" \
              "456 Elm Street\n" \
              "Chicago, IL 60601\n" \
              "United States"
print(long_string)

Output:

Emily Johnson
456 Elm Street
Chicago, IL 60601
United States

I have executed the above example code and added the screenshot below.

Split a Long String into Multiple Lines in Python backslashes

The backslash approach is useful when you want to split a string without adding extra indentation or when using parentheses or brackets is not convenient.

Read How to Split a String Every N Characters in Python?

4. Use the splitlines() Method

Python provides a built-in string method called splitlines() that allows you to split a multi-line string into a list of individual line sources. Here’s an example:

long_string = """
Michael Brown
789 Oak Avenue
Los Angeles, CA 90001
United States
"""
lines = long_string.splitlines()
for line in lines:
    print(line)

Output:

Michael Brown
789 Oak Avenue
Los Angeles, CA 90001
United States

The splitlines() method is handy when you need to process each line of a multi-line string separately.

Read Python Split Regex

Conclusion

In this tutorial, I explained how to split a long string into multiple lines in Python. I discussed some methods like using parentheses, brackets, or braces, triple quotes, backslashes, splitlines() method.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.