Fixing Python Indentation
A software developer gives a quick tutorial on how to properly and quickly format your Python code using a handy keyboard shortcut.
Join the DZone community and get the full member experience.
Join For FreeSome times, or maybe evem most of the time, some of us face the problem of indentation when it comes to writing code in Python. The weird thing is that despite the fact that you think you used the right indentation in your code editor, you still get stuck in an indentation error. This can be so frustrating because I can tell you confidently that I have experienced this so many times. Good news! I have a solution to any indentation problem you might face when coding with Python. This solution works all the time, again I mean all the time. I have experimented with this and I can confidently say it works all the time.
When you take a look at this image the first thing you should do is select all the code and press shift + tab to align the code block to the left, then hit the tab button the number of times you want to indent to the right. That is it, your indentation problem just got solved. For multiple blocks of code such as this image repeat the step above by pressing shift + tab to align the blocks of the code to left and hit tab the number of times you want to indent to the right and that solves the indentation issue.
Opinions expressed by DZone contributors are their own.
Comments