0

I am making a dictionary in Python using IntelliJ and the second entry is giving me an error PEP 8(line too long (155 > 120 characters).

classic_common = {'Abusive Sergeant': 'ADD ME TO YOUR DECK, YOU MAGGOT!',
                  **'Acolyte of Pain': "He trained when he was younger to be an acolyte of joy, but things didn't work out like he thought they would",**

How can I wrap the string so it still functions and appears readable.

Thanks.

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 2
    You should first wrap your dictionary properly so each key, value is on its own line. Then all you should have to do in IntelliJ is just hit enter at the string position you want to break the line and it should take care of the quotes for you and alignment. Just like this -> https://imagebin.ca/v/3Ozn3XAXQjJw – idjaw Jun 06 '17 at 02:54
  • Possible duplicate of [How to have the formatter wrap code with IntelliJ?](https://stackoverflow.com/questions/3837959/how-to-have-the-formatter-wrap-code-with-intellij) – Jeeter Jun 07 '17 at 16:21

1 Answers1

0

Without getting into how to get IntelliJ to automatically to do this, here's an explanation on how to wrap long lines (including long strings) in Python in general:

In Python, a single statement can be written out over multiple lines by using either explicit or implicit line joining.

In the former, a backslash ('\', "line continuation character") is used to signal the end of a line. For example (as if entered into the Python Interpreter):

>>> a = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 \
... + 9 + 10
>>> print(a)
55

On the other hand, implicit line joining allows expressions on multiple lines within parentheses, square brackets, or curly braces to be automatically joined together. In other words, any amount of white space is treated the same within these. For example (using poor style for illustrative purposes):

>>> a = [1, 2, 3, 4, 5, 6  # comments are allowed too
... # as are empty lines
...
... # and even explicit line breaks
... \
... , 7, 8]
>>> print(a)
[1, 2, 3, 4, 5, 6, 7, 8]

As it turns out, you were already using implicit line joining in your code snippet when you put your key-value pairs on separate lines.

In addition to implicit line joining, you can take advantage of the fact that Python concatenates consecutive strings with no operator between them. One more example:

>>> a = 'The quick brown' 'fox'
>>> b = 'The quick brown' + 'fox'
>>> print(a==b)
True

Putting this all together, since you are within curly brackets, you can close your string at any point and then place the next part of the string anywhere on the next line. The white space agnosticism within the brackets means that you can align the beginning of each line of the string (notice how "He", "of", and "thought" are aligned below).

Thus can you get something like the answer from idjaw's comment:

>>> classic_common = {'Abusive Sergeant': 'ADD ME TO YOUR DECK, YOU MAGGOT!',
                      'Acolyte of Pain': "He trained when he was younger to be an acolyte " 
                                         "of joy, but things didn't work out like he " 
                                         "thought they would",
                      'some_other_key': 'some other value'}
D. Gillis
  • 670
  • 5
  • 8