Breaking a line of python to multiple lines? [duplicate]

Style guide (PEP-8) says:

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. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

Method 1: Using parenthesis

if (number > 5 and
        number < 15):
    print "1"

Method 2: Using backslash

if number > 5 and \
number < 15:
    print "1"

Method 3: Using backslash + indent for better readability

if number > 5 and \
        number < 15:
    print "1"

Leave a Comment