There are multiple ways in which you can format code in Visual Studio Code IDE (also known as VS Code),
Option 1: Using Keyboard Shortcut
For Windows:Keyboard Shortcut: Shift + Alt + F
For Mac (macOS):Keyboard Shortcut: ⌥ Shift + Option + F
For Linux:Keyboard Shortcut: ⌥ Shift + Option + I
Note the first time you use the formatting shortcut you will get a VS Code Alert saying "Formatter autopep8 is not installed. Install?" do say YES or you can install it using pip command manually.

/opt/homebrew/bin/python3 -m pip install -U autopep8
Collecting autopep8
Downloading autopep8-1.7.0-py2.py3-none-any.whl (45 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.6/45.6 kB 1.1 MB/s eta 0:00:00
Collecting pycodestyle>=2.9.1
Downloading pycodestyle-2.9.1-py2.py3-none-any.whl (41 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.5/41.5 kB 2.3 MB/s eta 0:00:00
Collecting toml
Downloading toml-0.10.2-py2.py3-none-any.whl (16 kB)
Installing collected packages: toml, pycodestyle, autopep8
Successfully installed autopep8-1.7.0 pycodestyle-2.9.1 toml-0.10.2
Option 2: Using Mouse:
Be on your Python code file and do a right-click and select: Format Document

You can also select a section of your code and format it by selecting "Format Section"
Option 3: Using Palette Command:
Another way is to use the Palette Command,
Open Palette,
For Windows: Control + Shift + P
For Mac: Command + Shift + P
Now search for Format Document and select to format.
Do share in the comments if you know of any other ways or tricks to format Python code in VS Code!
Have Questions? Post them here!
- Comments in Python Programming
- tkinter - Hello World! Program
- How to install Python 3.11 on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- Change label (text) color in tkinter
- Python Hello World! Program with code example (snippet)
- Calculate discount amount python code
- Take input argument from command line in Python Programming
- How to pip install Python Modules in VSCode
- Tkinter - add x and y padding to label text
- Python raise error with message example
- Check if String Contains a Substring - Python
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- What does b prefix before a String mean in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to List all Packages installed using pip [Python]
- Convert Float to String in Python
- Change the background of Tkinter label or text
- How to Install Python Modules in VS Code
- Indent Python code in Notepad++
- Validate email address in Python using regular expression (regex)
- Python: Fix command not found pip or pip3 on zsh shell
- TypeError: must be str, not int [Fix Python]
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python - Convert float to String
- Cannot open or preview pdf with view only and restricted download access in Microsoft Teams - Teams
- Microsoft Teams Error code - 6 issue - Teams
- How to Split a String in Python? - Python
- Change Home Page on Safari for Mac (macOS) - MacOS
- [Solution] fatal: not a git repository (or any of the parent directories): .git - Git
- How to get Client IP address using Java Code Example - Java
- [fix] JSON Expected BEGIN_OBJECT but was STRING at line 1 column 1 - Java
- Word wrap text in Notepad++ - NotepadPlusPlus