How do you spread a long statement over multiple lines in python


The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. 

 Example of paranthesized line break:

 list(
    "Hello"
    )

This will give the output:

['H', 'e','l', 'l', 'o']

 Example of back-slashed line break:

 print 'This s a really long line,', \
    'but we can make it across multiple lines.'

 This will give the output:

This is a really long line, but we can make it across multiple lines.

 Both of these can be used, you may choose to use either depending on which looks more readable to you. 

How do you spread a long statement over multiple lines in python

Updated on 30-Sep-2019 08:29:05

  • Related Questions & Answers
  • How to write long strings in Multi-lines C/C++?
  • How to wrap python object in C/C++?
  • How to allow long and unbreakable words to be broken and wrap to the next line in JavaScript?
  • How long does it take to learn Python?
  • How to convert Long array list to long array in Java?
  • How to match pattern over multiple lines in Python?
  • How to eliminate repeated lines in a python function?
  • Program to find how many lines intersect in Python
  • How to create a long multi-line string in Python?
  • Convert long primitive to Long object in Java
  • Lines and Indentation in Python
  • How to write multiple lines in text file using Python?
  • How to delete lines from a Python tkinter canvas?
  • How to word-wrap text in Tkinter Text?
  • How do I wrap a string in a file in Python?

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())

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

How do you break a long statement into multiple lines in Python?

Break a long line into multiple lines using backslash According to PEP8 coding convention, each line should be limited to maximum 79 characters for better readability.

How do you write a multiline statement in Python?

Multi-line Statement in Python: In Python, the statements are usually written in a single line and the last character of these lines is newline. To extend the statement to one or more lines we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation character slash “\”.

How do you handle long lines in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

How do you continue a long string in Python?

Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.