Search code examples
pythonpython-3.xstringf-string

How can I split up a long f-string in Python?


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?


Solution

  • 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.