Python
PEP-8
Code Style
Line Length
Programming Guidelines

Why does PEP-8 specify a maximum line length of 79 characters?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

PEP 8 recommends a maximum line length of 79 characters because readability depends not only on the code itself, but also on the environments where people read, review, and edit that code. The number comes from old 80-column displays, yet the underlying reason still matters in terminals, diffs, side-by-side reviews, and documentation.

The Historical Origin

The 79-character rule comes from the older 80-column convention. Leaving one column free reduced edge effects and made terminals, printers, and text markers less awkward to use.

The exact number is historical, but the broader idea survives: code that fits comfortably inside a moderate width is easier to scan and compare.

Why the Rule Still Helps Today

Even with wide monitors, Python code is often read under constrained conditions:

  • split editor panes
  • side-by-side diffs
  • code review tools
  • terminal windows
  • documentation excerpts

Long lines force horizontal scrolling or make these views cramped. Shorter lines keep structure visible.

python
1def summarize_report(title, author, created_at, total_items, failed_items):
2    return (
3        f"Report {title} by {author} at {created_at}: "
4        f"{failed_items} of {total_items} items failed"
5    )

That wrapped version is usually easier to review than one long line stretching off the screen.

Why 79 and Not 100 or 120

There is no mathematical law behind 79. It is a conservative default chosen by the Python style guide. Teams can and do use other values such as 88, 100, or 120 depending on their tooling and preferences.

What PEP 8 optimizes for is portability of readability. A shorter default works well in more situations, especially when code is shown in email patches, terminals, or split views.

PEP 8 also traditionally recommends 72 characters for comments and docstrings because prose wraps differently from code and becomes harder to read when lines get wide.

The Spirit of the Rule Matters More Than the Number

Style guides are tradeoffs. For example, Black is widely used with an 88-character default. That does not mean PEP 8 is obsolete. It means teams choose slightly different points on the same readability curve.

The real principle is this: avoid lines so long that structure disappears and review becomes unpleasant.

Good Wrapping Is Better Than Blind Wrapping

Following the line limit should not produce awkward code. Break lines where the structure stays obvious.

python
1result = some_function(
2    first_argument,
3    second_argument,
4    long_keyword_name=some_value,
5)

That is better than forcing a break in the middle of an expression that becomes harder to follow than the original long line.

When Deviation Is Reasonable

Sometimes a project chooses a different limit because of generated code, domain-specific naming, or standardized tooling. That is fine if the team applies the choice consistently.

What matters is deliberate readability, not worship of a single number.

Common Pitfalls

A common mistake is treating 79 as a magical value rather than as a readability-focused convention.

Another mistake is allowing lines to grow very wide simply because modern monitors can display them. Reviewers and future maintainers often read code in narrower contexts.

It is also possible to wrap too aggressively. If a 79-character limit produces confusing line breaks, the code itself may need refactoring, not just formatting.

Summary

  • PEP 8 uses 79 characters mainly for readability and tooling friendliness.
  • The rule has historical roots in 80-column displays but still helps in modern workflows.
  • Shorter lines improve terminal use, diffs, and side-by-side reviews.
  • The exact number is a convention, not a law of nature.
  • The real goal is readable code with sensible line breaks.

Course illustration
Course illustration

All Rights Reserved.