Python – Line-breaking Expression in Python

pythonsyntax

I am beginner in python and facing this problem. So how i can break the below expression in 2-3 lines

totalIncome = (classACost * float(classASeatsSold)) + (classBCost * float(classBSeatsSold)) + (classCCost * float(classCSeatsSold))

Like this.

totalIncome = (classACost * float(classASeatsSold)) +

(classBCost * float(classBSeatsSold)) + 

(classCCost * float(classCSeatsSold))

Basic reason is i wanted to fit the line in 80 columns. And if i am not right about question Title please also suggest suitable title. Thanks in advance.

Best Solution

You always never have to use line continuation characters in python thanks to parentheses:

totalIncome = ( (classACost * float(classASeatsSold)) +
                (classBCost * float(classBSeatsSold)) +
                (classCCost * float(classCSeatsSold)) )

Which gives you the advantage of not having to remove the character in case you join the lines later. Same goes for strings:

longString = ( 'This is the one line '
               'being continued here and '
               'ending with a line break \n' )

You almost always can use parentheses instead of line continuation symbols and it just looks nicer.