Is it possible to break a long line to multiple lines in Python? What to do?

622    Asked by DylanForsyth in Python , Asked on Mar 1, 2021
Answered by Dylan Forsyth

Yes, It is possible to break a long line to multiple lines in Python. The preferred way is by using Python's implied line continuation inside parentheses, brackets and braces.

You can add an extra pair of parentheses around an expression if it is necessary, but sometimes using a backslash looks better. Also, make sure to indent the continued line appropriately.

An example that illustrates how to do implicit line continuation:

a = some_function(

    '1' + '2' + '3' - '4')



Your Answer

Interviews

Parent Categories