Python Forum
How to do line continuation in Jupyter Notebook?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do line continuation in Jupyter Notebook?
#1
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
Reply
#2
remove comment from line 1 and then indent line 2
Mark17 likes this post
Reply
#3
Unfortunately the continuation does not work for comments. You need to comment the second line also.
Mark17 likes this post
Reply
#4
(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?
Reply
#5
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.
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 872 Nov-15-2023, 06:56 PM
Last Post: jst
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 626 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 36,272 Feb-12-2023, 01:41 PM
Last Post: jp21in
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 895 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Graphs from Jupyter notebook to word Scott 0 835 Nov-28-2022, 06:16 AM
Last Post: Scott
  Opening an empty Jupyter notebook in VSCODE Krischu 3 1,771 Mar-09-2022, 01:57 PM
Last Post: Larz60+
  Jupyter Notebook as IDE bytecrunch 0 1,579 Sep-12-2021, 07:41 PM
Last Post: bytecrunch
  [solved] unexpected character after line continuation character paul18fr 4 3,292 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  It says there is no keras in jupyter notebook. it is wrong there is a keras install Led_Zeppelin 0 1,849 Apr-27-2021, 08:29 PM
Last Post: Led_Zeppelin
  importing matplotlib in jupyter notebook Led_Zeppelin 4 3,400 Feb-25-2021, 07:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020