Python Forum

Full Version: How to do line continuation in Jupyter Notebook?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

This:

#print('Date is {}, DTE is {}, EXP is {}, STRIKE is {}, PRICE is {}, VOL is {}, \
OI is {}, and DELTA is {}'.format(DATE, DTE, EXP, STRIKE, PRICE, VOL, OI, DELTA))


results in:

File "<ipython-input-70-20e6b91ee4d8>", line 2
OI is {}, and DELTA is {}'.format(DATE, DTE, EXP, STRIKE, PRICE, VOL, OI, DELTA))
^
SyntaxError: invalid syntax

It's one commented line with a backslash [trying] to indicate that what follows is a continuation of the same line.

What am I doing wrong with regard to the backslash?

Mark
remove comment from line 1 and then indent line 2
Unfortunately the continuation does not work for comments. You need to comment the second line also.
(Sep-22-2021, 07:55 AM)ibreeden Wrote: [ -> ]Unfortunately the continuation does not work for comments. You need to comment the second line also.

Did not know that. Thanks.

For a regular line, after the backslash can I indent however I please or do I need to use four spaces?
You are free to use the indentation you like on continuation lines. But good programmers do it according to PEP 8, which contains guidelines about formatting of code. Read the details in: Indentation.
In short: use at least the same indentation as the starting line or 4 spaces more. Or more spaces if it helps clarifying the coherence of the code.