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!
- tkinter - Hello World! Program
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew
- How to install SpaCy (NLP Library) on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- How to uninstall pip Python packages
- 3 Ways to find if element is present in a List in Python
- How to Convert Python String to DateTime Object
- Python f-strings Formatted String Literals Syntax and Examples
- Where does brew install python in macOS
- Take input argument from command line in Python Programming
- Advanced print() Function Tutorial and Techniques for Python Developers
- How to add borders to tkinter label text
- Whats new in Python 3.10 Pre-release
- Float built-in function in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- How to check if Key Exists in Python Dictionary?
- Read a file line by line in Python Program
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- What is the Max and Minimum Value of int type in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- Fix: TypeError: can only concatenate str (not int) to str in Python
- How to take user input from the console in a Python program
- [Fix] TypeError: str object is not callable in Python
- 3 Python program to add two numbers
- How to delete a file using Python code example
- fill_parent vs match_parent vs wrap_content - Android
- [Java] Bad return type in lambda expression: int cannot be converted to boolean - Java
- Cannot open or preview pdf with view only and restricted download access in Microsoft Teams - Teams
- Zsh Shell: Custom alias that you may find useful - MacOS
- vi undo redo command [Examples] - Mac-OS-X
- [Java] Read a File with UTF-8 Encoding - Java
- Save cURL Command Output to a external file - cURL
- Keyboard shortcut to close tab in Notepad++ - NotepadPlusPlus