Skip to main content

What is code formatting?

Writing clean, consistent code is important. Code formatting means organizing your code so it’s easy to read and follows Python’s style guidelines.

Basic formatting principles

Follow these basic rules for readability:

Indentation

Use 4 spaces for indentation (not tabs):
def greet(name):
    if name:
        print(f"Hello, {name}!")
    else:
        print("Hello!")

Spacing

Add spaces around operators and after commas:
# Good
x = 1 + 2
numbers = [1, 2, 3, 4]

# Bad
x=1+2
numbers=[1,2,3,4]

Blank lines

Use blank lines to separate functions and logical sections:
def first_function():
    pass


def second_function():
    pass

Line length

Keep lines under 80-100 characters. Break long lines for readability:
# Good - readable
long_string = (
    "This is a very long string that "
    "spans multiple lines for readability"
)

# Bad - too long
long_string = "This is a very long string that spans multiple lines for readability but keeps going and going"
Later in the course, you’ll learn about Ruff - a tool that automatically formats your code. But understanding these basics first helps you write better code naturally.

Automatic formatting with Ruff

Want to format your code automatically? Check out the Ruff guide in the tools section:

Code quality with Ruff

Automatically format and lint your code

What’s next?

Now that you understand formatting, let’s learn about variables!

Variables

Learn how to store and use data in Python