Why Code Wraps at 80 Columns: Punched Cards to PEP 8
Code wraps at 80 columns because an IBM punched card held 80 characters, and the terminals that replaced cards kept the same width. Python's PEP 8 inherited the habit and trimmed it to 79. Today the number is a convention, not a technical limit, and you can choose your own.
The 80-column punched card
The IBM 12-row/80-column punched card format came to dominate the industry, and a line of an IBM punched card consisted of 80 characters. One card, one line of code. A line longer than 80 characters simply didn't fit.
Cards themselves are older than that format. Herman Hollerith built a machine-readable recording method for the 1890 U.S. census, and those early cards had round holes, 12 rows and 24 columns. His Tabulating Machine Company, founded in 1896, merged into the Computing-Tabulating-Recording Company in 1911, which was renamed IBM in 1924.
Fortran shows how literal the card was. Its fixed form used 72 columns for source code, but the form was 80 characters wide: the last 8 positions were reserved as an identification sequence.
From cards to terminals
When screens replaced cards, the width came along for the ride. The IBM 3270 terminal displayed 80 columns of text in text mode, for compatibility with existing software. DEC's VT52 showed 80 columns and 24 rows, and the VT100, introduced in August 1978, offered 80x24 with an optional 132-column mode.
The same width reached personal computers. The original text mode of the IBM PC was 80x25, and virtual terminals still most often display 80x24. Other widths had their own origins: 132 columns came from mainframe line printers, and American teletypewriters could type only 72 characters per line.
What PEP 8 says: 79 and 72
PEP 8 is direct: "Limit all lines to a maximum of 79 characters." For docstrings and comments, it says the line length should be limited to 72 characters. It gives no separate reason for 72.
The reason it gives for 79 is practical. The limits are chosen to avoid wrapping in editors with the window width set to 80, even if the tool places a marker glyph in the final column when wrapping lines. One spare column keeps a line from spilling over.
It also argues for narrow lines on their own merits:
- Limiting the editor window width makes it possible to have several files open side by side.
- It works well with code review tools that show the two versions in adjacent columns.
- The default wrapping in most tools disrupts the visual structure of the code.
- Some web-based tools may not offer dynamic line wrapping at all.
For long lines, PEP 8 prefers Python's implied continuation inside parentheses, brackets and braces:
total = (first_value
+ second_value
+ third_value)PEP 8 also admits that some teams strongly prefer a longer line length. They can go up to 99 characters, as long as comments and docstrings still wrap at 72. The standard library stays conservative at 79 and 72.
Other style guides
PEP 8 is one of many. Wikipedia's comparison lists these limits:
| Guide | Limit | |---|---| | Ada | 72 | | Python (PEP 8) | 79 | | Google C++, Google Python, Java, Ruby, OpenBSD | 80 | | Linux kernel | 80, preferred but not a hard requirement | | Black | 88 | | Fortran (until 2023) | 132 |
With widescreen monitors now common, some projects have relaxed their limits, as the Linux kernel and FreeBSD have.
Hard wrapping versus soft wrapping
An 80-column rule is about hard wrapping: real line breaks stored in the file. Soft wrapping only changes how an editor displays a long line, and the file keeps one line. Code usually wants hard wraps, because diffs, reviews and terminals all work line by line. If you are unsure how breaks behave in a text file, see hard line breaks in a plain-text file. Prose in Markdown is usually fine soft-wrapped. Many plain text tools work line by line, so it helps to know which kind of break your file holds.
Choosing a wrap width today
Pick one number per project, and write it down. If the project already has a style guide, follow it. If you are on your own, 79 or 80 keeps files readable side by side and in two-column diffs, while 88 or 99 gives more room for long names. Turn on a ruler or guide line at your chosen column so you notice when a line passes it. This matters for scripts too, since a text file becomes a script the same way whatever its width.
How Carets fits in
Carets is a native notes and code editor for iPhone, iPad, and Mac. It handles Markdown, plain text and code in one place, so the same app holds your README, your config file and a Python script you want to check against a 79-character rule. Syntax highlighting keeps code readable on a narrower iPhone or iPad screen, and files, projects and tags keep snippets organized. Your files stay plain text, so line width is a choice you make, and nothing about them locks you in.
Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
Why is the PEP 8 limit 79 and not 80?
PEP 8 says the limits are chosen to avoid wrapping in editors with the window width set to 80, even if the tool places a marker glyph in the final column when wrapping lines. Leaving that last column free is the reason.
Why are Python docstrings and comments limited to 72 characters?
PEP 8 states the 72-character limit for docstrings and comments but gives no separate reason for the number. Wikipedia notes that many plain-text documents use 72 by tradition, as RFC 678 does.
Do I have to wrap at 80 today?
No. PEP 8 itself allows teams to go up to 99 characters if comments and docstrings still wrap at 72, and the Linux kernel treats 80 as preferred, not a hard requirement.