97

I am getting a line too long PEP 8 E501 issue.

f'Leave Request created successfully. Approvers sent the request for approval: {leave_approver_list}'

I tried using a multi-line string, but that brings in a \n, which breaks my test:

f'''Leave Request created successfully.
Approvers sent the request for approval: {leave_approver_list}'''

How can I keep it single line and pass PEP 8 linting?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tread
  • 10,133
  • 17
  • 95
  • 170
  • 1
    This is indeed a partial duplicate, but the duplicate does not pertain to f strings, a _slight_ modification is needed to that answer. – cs95 Feb 20 '18 at 08:58

2 Answers2

168

Use parentheses and string literal concatenation:

msg = (
    f'Leave Request created successfully. '
    f'Approvers sent the request for approval: {leave_approver_list}'
)

Note, the first literal doesn't need an f, but I include it for consistency/readability.

tread
  • 10,133
  • 17
  • 95
  • 170
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 14
    To add to that: [_"The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation."_](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) – moooeeeep Feb 20 '18 at 09:15
  • 1
    The first line should not have an `f` – Aidan H Apr 09 '21 at 18:19
  • 11
    @AidanH why *should* it not? It makes no difference, so it comes down to a style choice. I prefer the `f` here so they line up. I can understand why people would feel otherwise, though. I actually *noted* this in the answer. – juanpa.arrivillaga Apr 09 '21 at 18:32
  • 2
    Some tools will complain if you have an f-string with no actual interpolation (pylint or mypy does this, I think). You may need to tell the tool to not complain about that line. – paxdiablo Jul 12 '22 at 08:45
  • @paxdiablo `pylint` does, but I don't hink `mypy` does – juanpa.arrivillaga Jul 12 '22 at 16:57
  • @juanpa.arrivillaga *does* `pylint`? I don't think that does either, for https://github.com/PyCQA/pylint `v2.10.2` at least – ijoseph Sep 28 '22 at 22:07
  • When trying this approach, it generated string msg = 'Leave Request created successfully. " "Approvers sent the request for approval: list' . Note the spaces in quotes in the middle. Why are these present in the strings in my case on python 3.10.7? – MiroJanosik Mar 15 '23 at 20:42
  • @MiroJanosik you are going to have to provide a [mcve]. Just guessing, butdid you use single quotes to start the string, and then accidentally used double quotes to try to end it? In any case, that is absolutely not what happens with the above code in Python 3.10 – juanpa.arrivillaga Mar 15 '23 at 20:55
  • @juanpa.arrivillaga - sorry, I can't reproduce it anymore. – MiroJanosik Mar 23 '23 at 09:37
20

You will need a line break unless you wrap your string within parentheses. In this case, f will need to be prepended to the second line:

'Leave Request created successfully.'\
f'Approvers sent the request for approval: {leave_approver_list}'

Here's a little demo:

In [97]: a = 123

In [98]: 'foo_'\
    ...: f'bar_{a}'
Out[98]: 'foo_bar_123'

I recommend juanpa's answer since it is cleaner, but this is one way to do this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cs95
  • 379,657
  • 97
  • 704
  • 746