autopep8

autopep8 would auto-format your python script. not only the code indentation, but also other coding spacing styles. It makes your python script to conform PEP8 Style Guide.

pip install autopep8
autopep8 your_script.py    # dry-run, only print
autopep8 -i your_script.py # replace content

Update:

Many editors have pep8 plugins that automatically reformat your code right after you save the file. py-autopep8 in emacs

yapf

yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.

pip install yapf
yapf your_script.py    # dry-run, only print
yapf -i your_script.py # replace content

For more information, like formatting configurations, please read the README.rst on yapf github


Update 2:

Black

Black is much better than yapf. It's smarter and fits most complex formatting cases.

Answer from d2207197 on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and Output โ€” Python 3.14.3 documentation
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.
๐ŸŒ
Python
peps.python.org โ€บ pep-0008
PEP 8 โ€“ Style Guide for Python Code | peps.python.org
All identifiers in the Python standard library MUST use ASCII-only identifiers, and SHOULD use English words wherever feasible (in many cases, abbreviations and technical terms are used which arenโ€™t English).
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ formatting
Formatting Python in VS Code
November 3, 2021 - Open a Python file in VS Code. Right-click on the editor to display the context menu. Select Format Document With....
๐ŸŒ
GitHub
github.com โ€บ google โ€บ yapf
GitHub - google/yapf: A formatter for Python files ยท GitHub
Users calling this script directly should be careful to ensure that the path in the diff is correct relative to the current working directory. optional arguments: -h, --help show this help message and exit -i, --in-place apply edits to files instead of displaying a diff -p NUM, --prefix NUM strip the smallest prefix containing P slashes --regex PATTERN custom pattern selecting file paths to reformat (case sensitive, overrides -iregex) --iregex PATTERN custom pattern selecting file paths to reformat (case insensitive, overridden by -regex) -v, --verbose be more verbose, ineffective without -i -
Starred by 14K users
Forked by 900 users
Languages ย  Python
๐ŸŒ
Astral
docs.astral.sh โ€บ ruff โ€บ formatter
The Ruff Formatter - Astral Docs
The Ruff formatter is an extremely fast Python code formatter designed as a drop-in replacement for Black, available as part of the ruff CLI via ruff format. ruff format is the primary entrypoint to the formatter. It accepts a list of files or directories, and formats all discovered Python files:
๐ŸŒ
Google
google.github.io โ€บ styleguide โ€บ pyguide.html
Google Python Style Guide
Trailing commas in sequences of items are recommended only when the closing container token ], ), or } does not appear on the same line as the final element, as well as for tuples with a single element. The presence of a trailing comma is also used as a hint to our Python code auto-formatter Black or Pyink to direct it to auto-format the container of items to one item per line when the , after the final element is present.
๐ŸŒ
GitHub
github.com โ€บ psf โ€บ black
GitHub - psf/black: The uncompromising Python code formatter ยท GitHub
Stylistic changes will mostly be ... for new Python syntax. For more information please refer to The Black Code Style. Also, as a safety measure which slows down processing, Black will check that the reformatted code still produces a valid AST that is effectively equivalent to the original (see the Pragmatism section for details). If you're feeling confident, use --fast. Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in ...
Starred by 41.4K users
Forked by 2.7K users
Languages ย  Python
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ online_python_formatter.htm
Online Python Formatter | Tutorialspoint
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print "search --> searchObj.group() : ", searchObj.group() else: print "Nothing found!!"
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ fileformats.html
File Formats โ€” Python 3.14.3 documentation
The modules described in this chapter parse various miscellaneous file formats that arenโ€™t markup languages and are not related to e-mail. csv โ€” CSV File Reading and Writing- Module Contents, Diale...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_format.asp
Python String format() Method
Python Overview Python Built-in ... Python Certificate Python Training ... The format() method formats the specified value(s) and insert them inside the string's placeholder....
๐ŸŒ
Minifier
minifier.org โ€บ python-beautifier
Python Beautifier - Format & Beautify Python Code Online
Paste your Python code in the editor. Click the "Beautify" button. Get formatted Python code instantly in the output box.
๐ŸŒ
DeepSource
deepsource.com โ€บ blog โ€บ python-code-formatters
Python code formatters โ€ข DeepSource
November 2, 2020 - On doing so, Black will format the files in-place. ... Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. ... Standard library imports. Related third party imports. Local application/library specific imports. You should put a blank line between each group of imports. isort sorts imports in your Python files automatically โ€” alphabetically, separated into sections, and by type.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ visualstudio โ€บ python โ€บ formatting-python-code
Reformat Your Python Code in Visual Studio - Visual Studio (Windows) | Microsoft Learn
January 7, 2026 - To format the whole file, select Edit > Advanced > Format Document. You can enable word wrapping in the Tools > Options pane under the All Settings > Languages > Python > General section.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Re-formatting a text.file using python - Python Help - Discussions on Python.org
June 24, 2022 - Hi all, I am quiet new in python, and I wish to have your help with re-formatting a text file. A portion of the input text.file is below: Date 1936 10 1 CHANGE โ€˜10โ€™ 1.0 CHANGE โ€˜101โ€™ 0.4516 It mainly contains two keywords: DATE (year month day) and CHANGE (following be a variable like โ€˜10โ€™ and a number like 1.0). What I want is to have it converted as the text.file below: Date 1 Oct 1936 CHANGE โ€˜10โ€™ good 1 CHANGE โ€˜101โ€™ bad 0.4516 Changes includes: 1. Date reform...
๐ŸŒ
University of Utah
web.physics.utah.edu โ€บ ~detar โ€บ lessons โ€บ python โ€บ fileio_formatting โ€บ node14.html
Writing to a file with formatting using write()
Next: Writing to a file Up: Writing to a file Previous: Writing to a file ยท If you want to control the formatting, there is another way. The example below illustrates how to write the float x and its square x*x to a file called output.
๐ŸŒ
GitHub
github.com โ€บ life4 โ€บ awesome-python-code-formatters
GitHub - life4/awesome-python-code-formatters: A curated list of awesome Python code formatters ยท GitHub
decrapify: some scripts that use pybowler.io for refactoring Python code. docformatter: formats docstrings to follow PEP 257. docstrfmt: a tool for automatically formatting reStructuredText in files and Python docstrings in a consistent way.
Starred by 379 users
Forked by 22 users
Languages ย  Shell
๐ŸŒ
JetBrains
jetbrains.com โ€บ help โ€บ pycharm โ€บ reformat-and-rearrange-code.html
Reformat and rearrange code | PyCharm Documentation
January 6, 2026 - You can reformat a part of code, the whole file, a group of files, a directory, and a module. You can also exclude part of code or some files from the reformatting. PyCharm adheres to PEP 8 rules and requirements for arranging and formatting Python code.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals: ... You can perform Python operations inside the placeholders.