alper
alper

Reputation: 3410

How to handle formatting a regular string which could be a f-string [C0209]

For the following line:

print("{0: <24}".format("==> core=") + str(my_dict["core"]))

I am getting following warning message:

[consider-using-f-string] Formatting a regular string which could be a f-string [C0209]

Could I reformat it using f-string?

Upvotes: 5

Views: 11065

Answers (2)

Pierre.Sassoulas
Pierre.Sassoulas

Reputation: 4282

You could change the code to print(f"{'==> core=': <24}{my_dict['core']}"). The cast to string is implicit.

Upvotes: 6

Thomas
Thomas

Reputation: 899

In your case, the refactoring would look like this:

print(f"{'==> core=': <24}" + str(my_dict['core']))

Basically, instead of "{0:...}".format(bar) you write f"{bar:...}". (Note, you have to use single quotes inside of your f-string, since double quotes would terminate the string too early.)

Check out https://realpython.com/python-f-strings/ for a nice introduction to f-strings.

Upvotes: 2

Related Questions