Styling multiline 'if' statements in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
2 min

banner

# Styling multiline if statements in Python

The recommended style for multiline if statements in Python is to use parentheses to break up the if statement.

The PEP8 style guide recommends the use of parentheses over backslashes and putting line breaks after the boolean and and or operators.

main.py
if ('hi' == 'hi' and len('hi') == 2 and 2 == 2): # ๐Ÿ‘ˆ๏ธ Last lines with extra indentation print('success') # ๐Ÿ‘‡๏ธ All conditions on separate lines if ( 'hi' == 'hi' and len('hi') == 2 and 2 == 2 ): print('success')

styling multiline if statements

The code for this article is available on GitHub

The preferred way to wrap long lines according to the official PEP8 style guide is to use Python's implied line continuation inside parentheses, brackets and braces.

The guide doesn't recommend using backslashes for line continuation.

The guide also recommends putting line breaks after the boolean operators and and or.

# Using extra indentation to differentiate between conditions

The first example in the code sample uses extra indentation to differentiate between the conditions in the if statement and the code in the if block.

main.py
# โœ… GOOD if ('hi' == 'hi' and len('hi') == 2 and 2 == 2): # ๐Ÿ‘ˆ๏ธ Last lines with extra indentation print('success')

using extra indentation to differentiate between conditions

The code for this article is available on GitHub

This makes our code more readable than using the same level of indentation for the conditions and the if block.

main.py
# โ›”๏ธ BAD (same level of indentation for conditions and body of if statement) if ('hi' == 'hi' and len('hi') == 2 and 2 == 2): print('success')

# Moving all conditions in the if statement on separate lines

You can also move all of the conditions in the if statement on separate lines.

main.py
# โœ… GOOD if ( 'hi' == 'hi' and len('hi') == 2 and 2 == 2 ): print('success')

moving all conditions in the if statement on separate lines

The code for this article is available on GitHub
When using this approach, you don't have to use extra indentation for the conditions because the closing parenthesis separates the conditions from the body of the if statement.

For longer if statements, I find this a little easier to read than the previous example.

# Using backslashes for line continuation

Even though the PEP8 style guide discourages using backslashes for line continuation, it still is a perfectly valid syntax.

main.py
if 'hi' == 'hi' and \ len('hi') == 2 and \ 2 == 2: print('success')

using backslashes for line continuation

The code for this article is available on GitHub

When using this approach, make sure to add extra indentation for the last line(s) of conditions.

If you use the same level of indentation for the conditions and the body of the if statement, the code becomes difficult to read.

main.py
# โ›”๏ธ BAD (same level of indentation for conditions and `if` body) if 'hi' == 'hi' and \ len('hi') == 2 and \ 2 == 2: print('success')

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev