Write code in multiple lines python

Just like C, you can break a long line into multiple short lines. But in Python, if I do this, there will be an indent error... Is it possible?

asked Nov 13, 2010 at 12:17

Bin ChenBin Chen

59.1k53 gold badges139 silver badges182 bronze badges

1

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. 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.

Example of implicit line continuation:

a = some_function[
    '1' + '2' + '3' - '4']

On the topic of line breaks around a binary operator, it goes on to say:

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line.

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 [line breaks before the operator] is suggested.

Example of explicit line continuation:

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

answered Nov 13, 2010 at 12:20

Darin DimitrovDarin Dimitrov

1.0m266 gold badges3249 silver badges2911 bronze badges

3

There is more than one way to do it.

1]. A long statement:

>>> def print_something[]:
         print 'This is a really long line,', \
               'but we can make it across multiple lines.'

2]. Using parenthesis:

>>> def print_something[]:
        print ['Wow, this also works?',
               'I never knew!']

3]. Using \ again:

>>> x = 10
>>> if x == 10 or x > 0 or \
       x < 100:
       print 'True'

Quoting PEP8:

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.

answered Nov 13, 2010 at 12:26

user225312user225312

120k66 gold badges167 silver badges181 bronze badges

6

If you want to assign a long string to variable, you can do it as below:

net_weights_pathname = [
    '/home/acgtyrant/BigDatas/'
    'model_configs/lenet_iter_10000.caffemodel']

Do not add any comma, or you will get a tuple which contains many strings!

answered Apr 27, 2016 at 7:17

3

It works in Python too:

>>> 1+\
      2+\
3
6
>>> [1+
          2+
 3]
6

answered Nov 13, 2010 at 12:20

AbyxAbyx

11.7k5 gold badges41 silver badges75 bronze badges

When trying to enter continuous text [say, a query] do not put commas at the end of the line or you will get a list of strings instead of one long string:

queryText= "SELECT * FROM TABLE1 AS T1"\
"JOIN TABLE2 AS T2 ON T1.SOMETHING = T2.SOMETHING"\
"JOIN TABLE3 AS T3 ON T3.SOMETHING = T2.SOMETHING"\
"WHERE SOMETHING BETWEEN  AND "\
"ORDER BY WHATEVERS DESC"

kinda like that.

There is a comment like this from acgtyrant, sorry, didn't see that. :/

answered Jul 6, 2016 at 13:11

kotbegkotbeg

1891 silver badge4 bronze badges

1

DB related code looks easier on the eyes in multiple lines, enclosed by a pair of triple quotes:

SQL = """SELECT
            id, 
            fld_1, 
            fld_2, 
            fld_3, 
            ...... 
         FROM some_tbl"""

than the following one giant long line:

SQL = "SELECT id, fld_1, fld_2, fld_3, .................................... FROM some_tbl"

answered Sep 20, 2017 at 15:40

2

As far as I know, it can be done. Python has implicit line continuation [inside parentheses, brackets, and strings] for triple-quoted strings ["""like this"""] and the indentation of continuation lines is not important. For more information, you may want to read this article on lexical analysis, from python.org.

answered Nov 13, 2010 at 12:22

1

Not the answer you're looking for? Browse other questions tagged python line-breaks or ask your own question.

How do you put multiple lines in a code?

To break a single statement into multiple lines Use the line-continuation character, which is an underscore [ _ ], at the point at which you want the line to break.

How do you write 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.

Chủ Đề