Formatting Python — Why and How !

Xavier Jouvenot
2 min readMar 16, 2022

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain why you should format you python scripts and how to do it.

Self promotion: Here are a few social networks where you can follow me and check my work as a programmer and a writer 😉

personal website, Twitter, Dev.to, CodeNewbie, Medium, GitHub

Problematic (or why you should do it)

If you are not convinced that you should format you code, I really encourage you to go look at the previous article I did on Formatting and Automation, where I go in detail on why you should definitively format your code, and do it in a automatic way, to make your code more readable and make your team more productive since they won’t have to spend time arguing about Spaces VS Tabs.

Solution (or how to do it)

The short answer, for the people who don’t want to read through the entire article (I know you do that! I do it too 😆) is to use the tool named black:

# Installation of the formatting tool on any platform
pip install black # with python3 !
# Run the formatting tool on a python script
black <path/to/python/script.py>

In the code above, we start by installing the tool black, which is described as “The uncompromising code formatter”. I only showed you the simplest way to use this tool, by passing it, as arguments the list of the files you want to format, but…

--

--