PyCharm
line length
code formatting
IDE settings
Python editor

How to set the maximum line length in PyCharm

Codemia Knowledge Hub ·

Open Settings (or Preferences on macOS), go to Editor > Code Style > Python, and set the Right Margin (columns) field to the line length you want. PyCharm then highlights lines that exceed it, and the reformat command uses it when wrapping code.

Whether you follow PEP 8 or your organization's own rules, a maximum line length is worth setting:

  1. Readability: lines that stretch across the screen are harder to read and make errors and logic harder to spot.
  2. Consistency: files and projects look the same.
  3. Collaboration: teams follow the same practice, which lowers the effort of switching between codebases.
  4. Tool compatibility: some tools and CI systems expect a maximum line length for proper display and evaluation.

Setting the right margin step by step

  1. Open Settings: File > Settings on Windows/Linux, or PyCharm > Preferences on macOS.
  2. Go to Code Style: in the dialog, open Editor > Code Style.
  3. Pick the language: select "Python" (or another language) in the left sidebar to set the line length for that language.
  4. Set the line length: enter your limit in the Right Margin (columns) field. PEP 8 recommends at most 79 characters for ordinary lines and 72 for comments and docstrings.
  5. Save: click Apply, then OK.

PEP 8 values

For a project that follows PEP 8, the targets are:

  • Code: 79 characters
  • Comments and docstrings: 72 characters

With the margin set, PyCharm highlights lines that run past it so you can reformat or break them up.

Reformatting code to fit the limit

  • Reformat Code: press Ctrl + Alt + L (Windows/Linux) or Cmd + Option + L (macOS) to reformat according to your code style, including line length.
  • Scope: the same Code Style settings control what reformatting touches, such as whether it applies to comments or keeps existing line breaks.

Sharing the setting with other editors

  • EditorConfig: an .editorconfig file in the project root can define line lengths and share them across editors and IDEs.
  • Prettier: mainly for web projects, but it can be integrated when you need one formatter across many file types.

Quick reference

StepAction
Open SettingsFile > Settings, or PyCharm > Preferences on macOS
Locate Code StyleEditor > Code Style
Select languageChoose the language, for example Python
Configure right marginSet Right Margin (columns) to the desired line length
Apply and saveClick Apply, then OK
Auto-format shortcutCtrl + Alt + L or Cmd + Option + L

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms