Python Forum
How to do line continuation in Jupyter Notebook? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to do line continuation in Jupyter Notebook? (/thread-34976.html)



How to do line continuation in Jupyter Notebook? - Mark17 - Sep-21-2021

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


RE: How to do line continuation in Jupyter Notebook? - Larz60+ - Sep-21-2021

remove comment from line 1 and then indent line 2


RE: How to do line continuation in Jupyter Notebook? - ibreeden - Sep-22-2021

Unfortunately the continuation does not work for comments. You need to comment the second line also.


RE: How to do line continuation in Jupyter Notebook? - Mark17 - Sep-22-2021

(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?


RE: How to do line continuation in Jupyter Notebook? - ibreeden - Sep-22-2021

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.