How do you continue a string on the next line in python?

In trying to obey the python style rules, I've set my editors to a max of 79 cols.

In the PEP, it recommends using python's implied continuation within brackets, parentheses and braces. However, when dealing with strings when I hit the col limit, it gets a little weird.

For instance, trying to use a multiline

mystr = """Why, hello there
wonderful stackoverflow people!"""

Will return

"Why, hello there\nwonderful stackoverflow people!"

This works:

mystr = "Why, hello there \
wonderful stackoverflow people!"

Since it returns this:

"Why, hello there wonderful stackoverflow people!"

But, when the statement is indented a few blocks in, this looks weird:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
wonderful stackoverflow people!"

If you try and indent the second line:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there \
            wonderful stackoverflow people!"

Your string ends up as:

"Why, hello there                wonderful stackoverflow people!"

The only way I've found to get around this is:

do stuff:
    and more stuff:
        and even some more stuff:
            mystr = "Why, hello there" \
            "wonderful stackoverflow people!"

Which I like better, but is also somewhat uneasy on the eyes, as it looks like there is a string just sitting in the middle of nowhere. This will produce the proper:

"Why, hello there wonderful stackoverflow people!"

So, my question is - what are some people's recommendations on how to do this and is there something I'm missing in the style guide that does show how I should be doing this?

Thanks.

  • Introduction to Python line continuation
  • Getting started with Python line continuation
  • Problems with single, double, and triple quotation marks
    • Examples of problems with single and double marks
    • Example of problem with triple quotation marks
  • Python line continuation with explicit line break operator
    • Examples with explicit line break operator
    • Examples of integers and float with explicit line break operator
  • Python line continuation with brackets()
    • Python line continuations of strings using brackets
    • Python line continuation of integers and floats using brackets
  • Summary
  • Further Reading

Introduction to Python line continuation

We cannot split a statement into multiple lines in Python by just pressing Enter. Instead, most of the time we use the backslash ( \ ) to indicate that a statement is continued on the next line. In this tutorial, we will learn how we can show the continuation of a line in Python using different techniques. We will see why we cannot use triple, double, or single quotation marks for the line continuation.

At the same time, we will cover different ways of line continuation including backslash, and brackets to show the continuation of a line in Python by taking various examples. In a nutshell, this tutorial contains all the details and methods that you need to learn in order to know how to perform line break

Getting started with Python line continuation

The preferred way of wrapping long lines is by using python's parentheses. These should be used in preference to using a backslash for line continuation. The line continuation operator, \ can be used to split long statements over multiple lines. In python, a backslash (\) is a continuation character, and if it is placed at the end of a line, it is considered that the line is continued, ignoring subsequent newlines. First, let us see what problems occur when we try to just quotation marks.

Problems with single, double, and triple quotation marks

In Python, quotation marks are used to define a string class. Something written inside quotation marks will be considered as a string. If we will write long sentences inside these quotation marks without explicitly defining any line continuation operator, and just press enters to go to the next line, the program will give an error. In the following sections, we will take different examples and see what kind of errors we get with the continuation of a line within quotation marks.

Examples of problems with single and double marks

Now let us take an example and see what kind of error we get when we go the next line inside quotation marks without explicitly defining any line continuation operator. See the example below:

# defining string value inside double quotation marks
mystring = "Wellcome to
        golinux, 
        here you can find
        programming tutorials"
# printing
print(mystring)

Output:

File "/home/uca/Downloads/python/main.py", line 2
          mystring = "Wellcome to
                                                 ^
SyntaxError: EOL while scanning string literal

Notice that we get an error because the quotation marks have to end in one line. We cannot start quotation marks in one line and end in another line. The same case is with single quotation marks. See the example below:

How do you continue a string on the next line in python?

Notice that again we get the error when we use the single quotation marks.

Example of problem with triple quotation marks

You have noticed that we cannot write multiple lines of text inside double and single quotation marks without any explicitly defining line continuation operator. In the case, of triple quotation marks, the program will not return any error, in fact, the output will be in different lines rather than being in one line only. See the example below:

# defining string value inside triple quotation marks
mystring = '''Wellcome to
golinuxcloud, 
here you can find
programming tutorials'''
# printing
print(mystring)

Output:

Wellcome to
golinuxcloud,
here you can find
programming tutorials

Although, this method does not return any error but the output is not the one that we require. We need the output to be line one line even if we wrote the string in different lines. For that purpose, we have to explicitly define the line continuation operators inside our string. In the following sections, we will cover some of them.

Python line continuation with explicit line break operator

The backslash \ operator, also known as an explicit line break or line continuation operator, can be used to break a single continued long line into many smaller and easy-to-read lines of code. We can use it inside either single, double, or triple quotation marks and the output will e a single line rather than an error or multiple lines. In this section, we will take some examples and see how we can use this operator for Python line continuation.

Examples with explicit line break operator

Now let us take an example and see how we can use the break operator for the Python line continuation. See the examples below:

# defining string value inside single quotation marks
mystring1 = 'Wellcome to \
golinux, \
here you can find \
programming tutorials'
# defining string value inside double quotation marks
mystring2 = "Wellcome to \
golinux, \
here you can find \
programming tutorials"
# printing
print(mystring1)
print(mystring2)

Output:

Wellcome to golinux, here you can find programming tutorials
Wellcome to golinux, here you can find programming tutorials

Notice that this time we didn't get any error and the output is in one line because of the line continuation operator that we have used. The same output will be for the triple quotation marks. See the example below:

# defining string value inside triple quotation marks
mystring1 = '''Wellcome to \
golinux, \
here you can find \
programming tutorials'''
# printing
print(mystring1)

Output:

Wellcome to golinux, here you can find programming tutorials

Notice that even we have used the triple quotation marks, but still the output is in one line because of the line continuation operator.

Examples of integers and float with explicit line break operator

So far we have taken examples of strings only. Let us take an example of integers and add numbers together from a different line. See the example below:

# python line continuation with break operator
sum = 2 + \
        3 + \
            4 + \
                6
# printing sum 
print(sum)

Output:

15

Notice that we were able to add the numbers that were in the different lines by using the python line continuation operator. The same will be applied for floating points as well. See the example below:

# Python line continuation using 
sum = 5.6 + \
10.4 + 1.1 + \
20.3 + 2.2
# printing
print(sum)

Output:

39.60000000000001

Notice that the output is also a floating number that was obtained by adding different numbers from different lines.

Python line continuation with brackets()

Another method that can be used for the python line continuation is to enclose the lines inside ().  We will write the strings or the integers inside these brackets in multiple lines and the result will be only one line. In this section, we will take different examples by using brackets for the Python line continuation.

Python line continuations of strings using brackets

Now let us take an example and see how we can use brackets for the line continuation in Python. See the example below:

# Python line continuation using brackets
mystring = ('welcome ' +'to '
'golinuxcloud ' 
'programming '+ 'tutorials')
# printing
print(mystring)

Output:

welcome to golinuxcloud programming tutorials

Notice that by using brackets and the addition operator, we were able to write multiple lines of strings in one line as an output.

Python line continuation of integers and floats using brackets

Now let us take an example of integers datatype and see how we can use the brackets to add the numbers from different lines. See the python program below:

# Python line continuation using 
sum = (5 +
10 + 11 +
20 + 2)
# printing
print(sum)

Output:

48

Notice that we get the sum of the numbers that were in different lines. The same is with the floating numbers. See the example below:

# Python line continuation using 
sum = (5.6 +
10.4 + 1.1 +
20.3 + 2.2)
# printing
print(sum)

Output:

39.60000000000001

Notice that a sum is again a floating number.

Summary

Suppose that we have a long line that is difficult to read, so we want to split it into multiple lines without actually creating new lines. In Python, we have different techniques to perform line continuation. In this tutorial, we have learned about the kind of errors that can occur if we just use quotation marks and write a long sentence in multiple lines.

Then we discussed two different ways to perform break long lines using the backslash operator which is also called line continuation operator and the brackets '()' which can also be used for similar purpose in Python by taking various examples.

Further Reading

Python line continuation
Python doc strings in Python
Python built in methods

How do you go to the next line in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do you write multiple lines on a string in Python?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How do you break a long line in Python?

To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.

How do you add a line break in string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.