What is PEP 8?
PEP 8 establishes Python’s official style guide for the Python code in its standard library, codifying the principles of writing readable, maintainable Python code.
While written specifically for the Python standard library, PEP 8 has often been used as a basis for code formatting in other projects.
Key Aspects
Naming Conventions
snake_casefor functions and variablesPascalCasefor classesUPPERCASEfor constants
Whitespace Rules
- 4 spaces for indentation (no tabs)
- Two blank lines between top-level functions/classes
- One blank line between methods in classes
Line Length
- Maximum line length of 79 characters for code
- Maximum line length of 72 characters for docstrings/comments
Enforcing PEP 8
Modern Python projects rarely enforce PEP 8 manually. Ruff handles both linting (catching PEP 8 violations via its pycodestyle rules) and formatting in a single tool. Black is an older formatter that also enforces a PEP 8-compatible style. Both tools default to a line length of 88 characters rather than PEP 8’s 79, a pragmatic choice that has become the community norm. See How to configure recommended Ruff defaults for setup instructions.
Learn More
Get Python tooling updates
Subscribe to the newsletter