AppDividend
Latest Code Tutorials

How to Write Continue On Next Line in Python

0

To write continue on the next line without actual line breaking in Python, use either implied line continuation inside parentheses(brackets) or explicit line break ( / ). As per the official style, the PEP8 guide implied line continuation inside the parentheses method is recommended. Still, my advice is to use it according to your requirement because sometimes, the parentheses method is not recommended.

Python continue on next line

To write continue on the next line in Python, use the implied line continuation inside parentheses, brackets, and braces. You can also use the explicit line break ( / ) for line break because it is always a good practice since you explicitly tell the compiler that please don’t break the line here; it is in the continuation.

Syntax

if a == True and \
   b == False:

If you are using parentheses, then use the following syntax.

if (a == True and
   b == False):

Example

Let’s take an example based on the syntax.

We will use the explicit operator ( / ) to continue on the next line.

a = 1
b = 0

if a == True and /
   b == False:
    print("hello")

Output

hello

You can see that if condition is divided between two lines using / explicit identifier.

Backslashes have their use cases, and they are appropriate at many times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable.

The disadvantage of using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.

Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the forming the single logical line, removing the backslash and the following end-of-line character.

 

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.