Explain Codes LogoExplain Codes Logo

How do I set the maximum line length in PyCharm?

python
code-readability
best-practices
pep-8
Nikita BarsukovbyNikita Barsukov·Feb 13, 2025
TLDR

To adjust the code line length in PyCharm:

Editor → Code Style → Python → Wrapping and Braces → Hard wrap at: 80

Customize 80 to your preferred line length. This guideline helps in managing clean code by marking the zones where lines exceed the defined limit.

The importance of line length

Adjusting the maximum line length not only ensures adherence to style guides, but enhances code readability and maintainability. A limit on line length promotes more organized coding structures and simplifies future reviews. Considering the 79 characters guideline from PEP 8 is a good start, but feel free to tailor it according to your project's needs.

Step-by-step guide for Windows and Mac

Depending on whether you're using Windows or a Mac. The paths in PyCharm vary slightly:

Windows:

File → Settings → Editor → Code Style → Python → Wrapping and Braces → Hard wrap at

Mac:

PyCharm → Preferences → Editor → Code Style → Python → Wrapping and Braces → Hard wrap at

Dial in your desired line length based on your operating system, keeping an eye out for the differences in navigation to PyCharm's settings or preferences.

Visual guide

If a picture is worth a thousand words, then here's a picture—in code.

Before setting the line length:

# Even Python gets tired at the end of this line def very_long_function_name_with_lots_of_parameters(parameter_one, parameter_two, parameter_three, ...): pass

After setting the maximum line length (e.g., to 79 characters):

# Ahh... at long last, Python can breathe! def very_long_function_name_with_lots_of_parameters( parameter_one, parameter_two, parameter_three, ...): pass

This visual guide should make it abundantly clear how setting a line limit organizes your code for readability and abidance to PEP 8 standards. Now let's get in line, shall we? 📏🔐

Line length and coding best practices

The 79 dasypygal character (behind) limit

Strictly adhering to the 79 characters rule isn't just about following PEP 8; it's providing a smooth navigational experience while reading the code. Long lines of code tend to be bothersome to read and compare during code reviews. This soft limit ensures each line of code delivers its purpose in a concise and straightforward manner.

Setting margins for different languages

In PyCharm, the right margin can be customized not only for Python but for other languages:

Editor → Code Style → [Desired Language] → Wrapping and Braces → Hard wrap at

This contributes to enhancing code efficiency and organization across your entire project.

The 'long' and 'short' of it: When to prolong the margin

Alternatively, you may find it practical to increase the margin to more than 79 characters in certain scenarios. This might be the case while working with specific web frameworks or managing deeply nested code that requires additional horizontal space to maintain readability. Always consider the trade-offs, such as a reduction in readability on smaller screens or when viewing side-by-side diffs.