Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Getting Python Code Indentation Right

Give Feedback

Indentation is very important in Python and PEP 8 lays out a couple of rules for it:

  • Use four consecutive spaces to indicate indentation
  • Prefer spaces over tabs.

This lesson will show you how to apply these two rules and the different ways you can use indentation to improve readability.

W Patrick Jones on April 22, 2019

Nothing on multi-line string assignements? That and really long SQL statements are the ones that always confuse me on where to indent.

Joe Tatusko RP Team on April 22, 2019

I usually bring the indent to the where the opening quotation character is on the next line to keep it in a nice block.

For the long SQL statements I’ll generally start a multiline string with nothing in the first line, then have everything non-indented to keep it grouped like it would be outside of Python (if I can’t use something like SQLAlchemy to be more Pythonic)

Abby Jones on June 25, 2019

I’ve set up my VIM env to limit tabs to 4 spaces. Is that still okay?

Joe Tatusko RP Team on June 26, 2019

Of course! I have all my editors set to turn pressing the tab key into 4 spaces. No need to bash the spacebar 4 times everytime you need to indent. The big thing is what character is actually saved in the file, 4 spaces or one tab

Become a Member to join the conversation.