close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to split up a long f-string in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    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:

  2. split up a long f-string in python

    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:

Method 1

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.

Method 2

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.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read