Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (47.6k points)

I have some Python code that has an inconsistent indentation. There is a lot of mixture of tabs and spaces to make the matter even worse, and even space indentation is not preserved.

The code works as expected, but it's difficult to maintain.

How can I fix the indentation (like HTML Tidy, but for Python) without breaking the code?

1 Answer

0 votes
by (106k points)
edited by

To fix Python indentation you can use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation:

Also, change the Python (.py) files to use 4-space indents and no hard tab characters. Try to trim excess spaces and tabs from ends of lines, and also remove empty lines at the end of files. One important point always ensures the last line ends with a newline.

To know more about this you can have a look at the following video:-

If you wish to learn more about Python, visit the Python tutorial and Python Certification course by Intellipaat.

Related questions

0 votes
1 answer
asked Mar 23, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Apr 26, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
0 answers

Browse Categories

...