What is the line? You can just have arguments on the next line without any problems:

a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, 
            blahblah6, blahblah7)

Otherwise you can do something like this:

if (a == True and
    b == False):

or with explicit line break:

if a == True and \
   b == False:

Check the style guide for more information.

Using parentheses, your example can be written over multiple lines:

a = ('1' + '2' + '3' +
    '4' + '5')

The same effect can be obtained using explicit line break:

a = '1' + '2' + '3' + \
    '4' + '5'

Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.

Answer from Harley Holcombe on Stack Overflow
Top answer
1 of 11
1591

What is the line? You can just have arguments on the next line without any problems:

a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, 
            blahblah6, blahblah7)

Otherwise you can do something like this:

if (a == True and
    b == False):

or with explicit line break:

if a == True and \
   b == False:

Check the style guide for more information.

Using parentheses, your example can be written over multiple lines:

a = ('1' + '2' + '3' +
    '4' + '5')

The same effect can be obtained using explicit line break:

a = '1' + '2' + '3' + \
    '4' + '5'

Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go.

2 of 11
322

From PEP 8 -- Style Guide for Python Code:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

Another such case is with assert statements.

Make sure to indent the continued line appropriately.

The preferred place to break around a binary operator is after the operator, not before it. Some examples:

class Rectangle(Blob):

  def __init__(self, width, height,
                color='black', emphasis=None, highlight=0):
       if (width == 0 and height == 0 and
          color == 'red' and emphasis == 'strong' or
           highlight > 100):
           raise ValueError("sorry, you lose")
       if width == 0 and height == 0 and (color == 'red' or
                                          emphasis is None):
           raise ValueError("I don't think so -- values are %s, %s" %
                            (width, height))
       Blob.__init__(self, width, height,
                     color, emphasis, highlight)file_2.write(file_1.read())

Update

PEP8 now recommends the opposite convention (for breaking at binary operations) used by mathematicians and their publishers to improve readability.

Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted.

From PEP8: Should a line break before or after a binary operator?:

Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"[3].

Following the tradition from mathematics usually results in more readable code:

# Yes: easy to match operators with operands
income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest)

In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style is suggested.

[3]: Donald Knuth's The TeXBook, pages 195 and 196

๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ breaking-long-lines-code-python
Breaking up long lines of code in Python - Python Morsels
May 6, 2021 - If you have a very long line of code in Python and you'd like to break it up over multiple lines, you can continue on the next line as long as you're within braces or parentheses.
Discussions

Line continuation issue (new to python)
We can't spot a syntax error if you don't format your code, fam. More on reddit.com
๐ŸŒ r/learnpython
17
1
July 13, 2021
Pythonic way of printing multi-line strings
Hello all, Iโ€™m looking to find out what is the proper, pythonic way of printing multi-line strings, for example a message for a programs user. The issue Iโ€™ve come across concerns indentation; when print() is nested inside a statement such as an if-then or try-except, the call to print() ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
October 4, 2022
How to break line lines of code into multiple lines?
The PEP-8 section , for reference. I would chop up the long string like so: big_string = ("PROJCS['NAD_1983_StatePlane_Georgia_West_FIPS_1002_Feet'," "GEOGCS['GCS_North_American_1983'," "DATUM['D_North_American_1983'," "SPHEROID['GRS_1980',6378137.0,298.257222101]]," "PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]," "PROJECTION['Transverse_Mercator']," "PARAMETER['False_Easting',2296583.333333333]," "PARAMETER['False_Northing',0.0]," "PARAMETER['Central_Meridian',-84.16666666666667]," "PARAMETER['Scale_Factor',0.9999]," "PARAMETER['Latitude_Of_Origin',30.0]," "UNIT['Foot_US',0.3048006096012192]]") arcpy.DefineProjection_management(photoKeyFC, big_string) More on reddit.com
๐ŸŒ r/learnpython
8
3
December 20, 2018
Is there a way to continue a single variable reference in an f-String on a new line of code?
You can have multiline f-strings, just like regular multiline strings. >>> x, y = (1, 2) >>> print(f"""x = {x} ... y = {y}""") x = 1 y = 2 As always, the textwrap.dedent function is very useful here for making the multiline string's indentation look a little better. >>> print(textwrap.dedent(f"""\ ... x = {x} ... y = {y}""")) x = 1 y = 2 More on reddit.com
๐ŸŒ r/learnpython
5
1
June 30, 2021
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ how-to-line-break-in-python
A Comprehensive Guide on How to Line Break in Python | DataCamp
May 17, 2024 - Remember, indentation matters for ... ยท Explicit line continuation with backslashes is typically used when syntax requires continuing onto the next line or emphasizing clarity in long expressions or statements...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ line continuation issue (new to python)
r/learnpython on Reddit: Line continuation issue (new to python)
July 13, 2021 -

I have a weird issue; not sure if I'm being incompetent or missing something...

I am running Python 3.8, using the Spyder IDE and Anaconda 1.10. It's a work laptop, so I'm limited on what I can change.

I'm trying to set up a dictionary. I understood that python continues lines that are between parentheses, but but that doesn't seem to be happening.

So this statement works fine:

Fruits_Dict = {1: 'apple', 2: 'pear'}

But when I break it across multiple lines...

Fruits_Dict = {

1: 'apple',

2: 'pear'}

...I get an "unexpected EOF" error at the first '{'.

I'd like to break a longer dictionary across several lines for readability, so this is a problem. Is this an IDE issue or am I missing something basic?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-multi-line-statements
Multi-Line Statements in Python - GeeksforGeeks
June 30, 2025 - backslash (\) is used to split a long line of code into multiple lines called as explicit line continuation. It tells Python that the statement continues on the next line, preventing a syntax error.
๐ŸŒ
Rhino
developer.rhino3d.com โ€บ guides โ€บ rhinopython โ€บ python-statements
Rhino - Python Basic Syntax
You cannot split a statement into multiple lines in Python by pressing Enter. Instead, use the backslash (\) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line ...
Find elsewhere
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Write a Long String on Multiple Lines in Python | note.nkmk.me
May 19, 2025 - Even if you use backslashes for ...aaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ... Python allows implicit line continuation within parentheses (), brackets [], or braces {} โ€” meaning you can break a line without needing a ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-multi-line-statements
Python - Multi-Line Statements - GeeksforGeeks
May 10, 2023 - In this type of multi-line statement, we will be using the line continuation character (\) to split a statement into multiple lines. In this example, we are initializing the text, and the mathematical expression using the '\' sign which is the explicit line continuation to continue the same line in the multiple lines in python programming.
Top answer
1 of 9
39

Add a trailing backslash (\)

The trick is โ€“ similar to what you would do in bash, for example โ€“ to add a trailing backslash. For example, if I want to print a 1:

charon:~ werner$ python
>>> print 1
1
>>> print \
... 1
1
>>> 

If you write a \, Python will prompt you with ... (continuation lines) to enter code in the next line, so to say.

Side note: This is what automatically happens when you create a function or class definition, i.e. the times when you really need a new line, so there's never a really good use for that, or at least none that I know of. In other words, Python is smart enough to be aware that you need continuation lines when you are entering a new function definition or other similar constructs (e.g. if:). In these automatic cases, do note that you need to enter an empty line using \ to tell Python that you are done.

For everything else, you need to write one line after another. The way an interpreter works is that it, well, interprets every line that you feed it. Not more, not less. It will only "act" when it sees a newline, therefore telling the interpreter to execute what you gave it. The single backslash will prevent the interpreter from ever receiving a newline character (i.e. it won't know that you actually pressed Enter), but it will eventually receive one.

Python's interpreter has advanced capabilities when you use GNU readline, such as Emacs or vi-style keybindings to navigate within a line (e.g. Ctrl-A). Those however work only in the one current line. History is there as well, just try and press โ†‘.

What if I want to run complicated lines over and over?

You probably want to use proper source files if you want to execute more than one line of code at a time.

Or, use Jupyter notebooks, which offer a great, interactive way to create Python code with a built-in interpreter. You can write code as you would in a source code editor, but you can choose which lines are interpreted together. You can then run only parts of the code selectively. The best way is to just try and see if that fits your workflow.

2 of 9
13

How about using ;\? The semicolon signals the end of a command and the backslash signals that we are continuing on the next line. For example, type python at command line to get into Python interpreter, then

>>> x=0 ;\
... print(x) ;\
... x=4 ;\
... print(x)

should give an output of

0
4
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ pass-vs-continue-python
Pass vs. Continue in Python Explained | Built In
Continue output in Python. | Image: Suraj Gurav ยท When the condition num == 5 becomes True, the continue statement gets executed. The remaining code in the loop is skipped only for that iteration. Thatโ€™s why Iteration: 5 is missing from the above output. Therefore, the continue statement works opposite to the break statement. Instead of terminating the loop, it forces it to execute the next iteration of the loop.
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ python-how-to-define-multiline-string
How do I split the definition of a long string over multiple lines in Python? | Better Stack Community
To split a long string over multiple lines in Python, you can use the line continuation character, which is a backslash (\) at the end of the line. The string will continue on the next line as if it were a single line.
๐ŸŒ
Squash
squash.io โ€บ how-to-implement-line-break-and-line-continuation-in-python
How to Implement Line Break and Line Continuation in Python
In Python, line break and line continuation are used to split long lines of code into multiple lines for better readability and maintainability. Line breaks are used to split a single line of code into multiple lines, while line continuation is used to continue a statement onto the next line without causing a syntax error.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ operators โ€บ slash.html
Line Continuation โ€” Python Reference (The Right Way) 0.1 documentation
Breaks the line of code allowing for the next line continuation. ... A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on ...
๐ŸŒ
Doingmathwithpython
doingmathwithpython.github.io โ€บ breaking-long-lines-in-python.html
Breaking long lines in Python
November 4, 2015 - # Using parenthesis if (cond1 and cond2 and cond3 and cond4): # True block else: # False block # Using line continuation operator if cond1 and cond2 and cond3 \ and cond4: # True block else: # False block ยท By default, when calling functions you can just press enter and without doing anything more keep writing your statement over multiple lines. For example: ... When calling format() we put the arguments over separate lines. If you liked reading this article, you may also find it worth your time going over the Python style guide.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How to Break Out of Multiple Loops in Python | DigitalOcean
August 7, 2025 - The continue statement causes a program to skip certain factors that come up within a loop but then continue through the rest of the loop. When an external condition is triggered, the pass statement allows you to satisfy Pythonโ€™s syntactical requirement for a code block without performing ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Pythonic way of printing multi-line strings - Python Help - Discussions on Python.org
October 4, 2022 - Hello all, Iโ€™m looking to find out what is the proper, pythonic way of printing multi-line strings, for example a message for a programs user. The issue Iโ€™ve come across concerns indentation; when print() is nested inside a statement such as an if-then or try-except, the call to print() ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to break line lines of code into multiple lines?
r/learnpython on Reddit: How to break line lines of code into multiple lines?
December 20, 2018 -

Hello! I'm a novice when it comes to working with python, so I apologize for the extremely basic question. The IDE I am using (pyCharm) is giving me a PEP 8 error (line too long). For a lot of the code I am working on. I understand that this isn't a hard constraint of python. Just something to improve readability, which I would like to do. Currently I have a line (below) that shoots way off the screen, and it is just annoying to make changes to it. I can't figure out how to spread the line of code across multiple lines while still getting python to parse it correctly. I hope this make sense. Can anyone provide any pointers? Thanks for your help!

The current monstrosity:



    arcpy.DefineProjection_management(photoKeyFC, "PROJCS['NAD_1983_StatePlane_Georgia_West_FIPS_1002_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',2296583.333333333],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-84.16666666666667],PARAMETER['Scale_Factor',0.9999],PARAMETER['Latitude_Of_Origin',30.0],UNIT['Foot_US',0.3048006096012192]]")
    

    More or less what I'd like to see:
    
    "PROJCS['NAD_1983_StatePlane_Georgia_West_FIPS_1002_Feet',GEOGCS['GCS_North_American_1983',DATUM['D_North_
American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.017453292
5199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',2296583.333333333],PARAMETER['False_Northi

ng',0.0],PARAMETER['Central_Meridian',-84.16666666666667],PARAMETER['Scale_Factor',0.9999],PARAMETER['Latitude_Of _Origin',30.0],UNIT['Foot_US',0.3048006096012192]]")

๐ŸŒ
W3docs
w3docs.com โ€บ python
How can I do a line break (line continuation) in Python?
In Python, you can use the "\" character to indicate a line continuation. For example: ... You can also use parentheses, brackets, or braces to indicate a line continuation. For example: x = (1 + 2 + 3 + 4 + 5 + 6) y = [1, 2, 3, 4, 5, 6] z = ...