How to Add Line Breaks in Python Strings?

In this tutorial, I will explain how to add line breaks in Python strings. As a Python developer based in the USA, I encounter situations where I need to insert line breaks inside a string to improve readability or format the output in one of my projects to New York clients. We’ll explore various methods to achieve this, along with practical examples.

Add Line Breaks in Python Strings

Python provides various ways to achieve this task. Let us see some important methods.

Read How to Remove Commas from a String in Python?

1. Use Newline Character (\n)

The most common method to add a line break in a Python string is by using the newline character \n. This special character represents the end of a line and inserts a line break at that position.

Let’s consider an example where we want to display an address in the United States:

address = "John Doe\n123 Main Street\nNew York, NY 10001"
print(address)

Output:

John Doe
123 Main Street
New York, NY 10001

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

Add Line Breaks in Python Strings

In this example, we use \n to separate the name, street address, and city/state/zip code onto different lines.

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

2. Use Triple Quotes for Multi-line Strings

Another way to include line breaks in a string is by using triple quotes (""" or '''). Triple quotes allow you to define multi-line strings without explicitly using the newline character.

Here’s an example showcasing a famous quote by an American author:

quote = """
"The future belongs to those
who believe in the beauty of their dreams."
- Eleanor Roosevelt
"""
print(quote)

Output:

"The future belongs to those
who believe in the beauty of their dreams."
- Eleanor Roosevelt

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

How to Add Line Breaks in Python Strings

Triple quotes preserve the line breaks and formatting within the string.

Read How to Count Words in a String Using Python?

Break Long Lines of Code

When working with long strings or expressions, you might need to break them into multiple lines for better code readability. In Python, you can use parentheses () or backslashes \ to achieve this.

Let’s say we have a long string containing a famous speech by Martin Luther King Jr.:

speech = ("I have a dream that one day this nation will rise up "
          "and live out the true meaning of its creed: "
          "'We hold these truths to be self-evident, "
          "that all men are created equal.'")
print(speech)

Output:

I have a dream that one day this nation will rise up and live out the true meaning of its creed: 'We hold these truths to be self-evident, that all men are created equal.'

By using parentheses, we can split the long string into multiple lines without affecting the resulting string.

Read How to Replace a Character at a Specific Index in a String Using Python?

Handle User Input with Line Breaks

When dealing with user input that may contain line breaks, you can use the input() function and handle the line breaks accordingly.

For example, let’s create a program that allows users to enter a short bio:

print("Enter your short bio (press Enter twice to finish):")
bio = ""
while True:
    line = input()
    if line == "":
        break
    bio += line + "\n"

print("Your bio:")
print(bio)

Output:

Enter your short bio (press Enter twice to finish):
My name is Sarah Johnson.
I am a software engineer based in San Francisco.
I love coding and exploring new technologies.

Your bio:
My name is Sarah Johnson.
I am a software engineer based in San Francisco.
I love coding and exploring new technologies.

In this example, the user can enter multiple lines of text, and the program appends each line to the bio string, adding a line break (\n) after each line.

Read How to Remove Prefixes from Strings in Python?

Conclusion

In this tutorial, I have explained how to add line breaks in Python strings. I covered methods like using Newline character(\n) and using triple quotes for multi-line strings. I also discussed how to break long lines of code and handle user input line breaks.

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.