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

Viewed 2581

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?

2 Answers

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

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.

Related