How to Continue Long Strings on the Next Line in Python?

In this tutorial, I will explain how to continue long strings on the next line in Python. As a Python developer when working with lengthy strings in my Python code, I came across a scenario where I needed to break them up for better readability and maintainability. Then I explored more about this topic and I will share my findings in this article with examples and screenshots of executed example code.

Continue Long Strings on the Next Line in Python

Python provides several ways to continue a string on the next line without affecting the string’s content. Let us learn some important methods.

Read Convert Int to Bytes in Python

1. Use Parentheses to Split Long Strings

One common approach to continue a string over multiple lines in Python is by using parentheses. When you enclose a string within parentheses, you can freely add line breaks without disrupting the string’s integrity. Here’s an example:

name = "John Smith"
address = "123 Main St, New York, NY 10001"
message = (
    f"Dear {name},\n"
    f"Thank you for your recent purchase. "
    f"Your order will be shipped to:\n"
    f"{address}"
)
print(message)

Output:

Dear John Smith,
Thank you for your recent purchase. Your order will be shipped to:
123 Main St, New York, NY 10001

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

Continue Long Strings on the Next Line in Python

In this example, the message string is split across multiple lines using parentheses. The line breaks within the parentheses are ignored, and the string is treated as a single continuous string.

Read Convert DateTime to UNIX Timestamp in Python

2. Use Backslashes for Line Continuation

Another way to split a long string over multiple lines is by using the line continuation character, which is a backslash (\) at the end of each line. The backslash indicates that the string continues on the next line. Here’s an example:

customer_name = "Sarah Johnson"
email = "sarah.johnson@example.com"
subject = "Order Confirmation"
body = "Dear " + customer_name + ",\n" \
       "We are pleased to inform you that your order has been confirmed. " \
       "You will receive a shipping confirmation email once your order is dispatched.\n" \
       "If you have any questions, please reply to this email: " + email + ".\n" \
       "Best regards,\n" \
       "The Sales Team"
print(f"Subject: {subject}\n\n{body}")

Output:

Subject: Order Confirmation

Dear Sarah Johnson,
We are pleased to inform you that your order has been confirmed. You will receive a shipping confirmation email once your order is dispatched.
If you have any questions, please reply to this email: sarah.johnson@example.com.
Best regards,
The Sales Team

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

How to Continue Long Strings on the Next Line in Python

In this example, the body string is split into multiple lines using backslashes. The backslash at the end of each line indicates that the string continues on the next line.

Read Concatenate String and Float in Python

3. Use Triple Quotes for Multiline Strings

Python also supports multiline strings using triple quotes (either single quotes ''' or double quotes """). Triple quotes allow you to write a string that spans multiple lines without the need for explicit line continuation characters. Here’s an example:

disclaimer = '''
DISCLAIMER:
The information provided in this email is for general informational purposes only. 
It is not intended as a substitute for professional advice.
If you have specific questions or concerns, please consult with a qualified professional.
'''
print(disclaimer)

Output:

DISCLAIMER:
The information provided in this email is for general informational purposes only. 
It is not intended as a substitute for professional advice.
If you have specific questions or concerns, please consult with a qualified professional.

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

Continue Long Strings on the Next Line in Python triple quotes

In this example, the disclaimer string is defined using triple single quotes. The string spans multiple lines, and the line breaks are preserved in the output above.

Read How to Find the Largest and Smallest Numbers in Python?

Conclusion

In this tutorial, we how to continue long strings on the next line in Python. We learned how to use parentheses to split long strings , how to use backslashes for line continuation , and triple quotes for multiline strings. These techniques help improve the readability and maintainability of your Python code when dealing with lengthy strings.

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.