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:
- Readability: lines that stretch across the screen are harder to read and make errors and logic harder to spot.
- Consistency: files and projects look the same.
- Collaboration: teams follow the same practice, which lowers the effort of switching between codebases.
- Tool compatibility: some tools and CI systems expect a maximum line length for proper display and evaluation.
Setting the right margin step by step
- Open Settings:
File > Settingson Windows/Linux, orPyCharm > Preferenceson macOS. - Go to Code Style: in the dialog, open
Editor > Code Style. - Pick the language: select "Python" (or another language) in the left sidebar to set the line length for that language.
- 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. - Save: click
Apply, thenOK.
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) orCmd + 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
.editorconfigfile 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
| Step | Action |
| Open Settings | File > Settings, or PyCharm > Preferences on macOS |
| Locate Code Style | Editor > Code Style |
| Select language | Choose the language, for example Python |
| Configure right margin | Set Right Margin (columns) to the desired line length |
| Apply and save | Click Apply, then OK |
| Auto-format shortcut | Ctrl + Alt + L or Cmd + Option + L |
Related reading
- How do I solve error externally-managed-environment every time I use pip 3?
- How do I solve overfitting in random forest of Python sklearn?
- How do I sort a dictionary by key?
- How do I split a list into equally-sized chunks?
- How do I split a multi-line string into multiple lines?
- How do I split a string into a list of characters?
- How do I split a string into a list of words?
- How do I subtract one list from another?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.