How to split up a long f-string in python?

stephen picture stephen · Feb 20, 2018 · Viewed 17.4k times · Source

I am getting a line too long pep8 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 pep8 linting

Answer

juanpa.arrivillaga picture juanpa.arrivillaga · Feb 20, 2018

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.