Pylint: How to fix "c0209: formatting a regular string which could be a f-string (consider-using-f-string)"

Pylint: How to fix "c0209: formatting a regular string which could be a f-string (consider-using-f-string)"

Learn how to fix this new Pylint rule that raises: 'formatting a regular string which could be a f-string (consider-using-f-string)' error

Some weeks I ago I faced this problem in one of my projects after upgrading pylint to 2.11.

The error was:

script.py:7:8: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)

At first I found it very confusing; my code was the same and it'd been working fine before upgrading it. I decided to dig a little deeper and found this pull request on Pylint's github page.

It turns out, this is a new feature that landed on Pylint 2.11.0.

In this post, you will see 3 different ways to fix this "formatting a regular string which could be a f-string (consider-using-f-string)" error.

How to Fix "formatting a regular string which could be a f-string (consider-using-f-string)"

Before Python introduced f-strings, one could use % or the str.format method to format a string. Even though these methods are still valid, f-strings are slightly preferred now.

As a way of enforcing developers to make this migration, the new Pylint version raises this error when it detects the old way of formatting string.

To fix that you can either:

  • replace the old formatting method with a f-string
  • ignore the Pylint error

Replacing % or the str.format with a f-string

Let's consider this small script that uses both methods.

name = 'world'

a = 'my hello %s' % name

print(a)

b = 'again this name is {}'.format(name) 

print(b)

If we run Pylint 2.11.0+ on it, we get a few errors:

C0209: Formatting a regular string which could be a f-string (consider-using-f-string)

If it's OK for you to update to f-string, then that’s the recommended way. How you do that depends on how you're formatting your strings but in doubt you can check this article to learn the myriad ways you can use a f-string.

In my case, replacing % and str.format becomes:

name = 'world'

a = f'my hello {name}'

print(a)

b = f'again this name is {name}'

print(b)

If we re-run Pylint, we get:

c0209: formatting a regular string which could be a f-string (consider-using-f-string)

In the next section, we'll use flags to disable this error.

Ignoring the error using flags

You can also ignore the error instead of converting it to f-strings. To do that, you can either add a disabling flag at the top of the python file, or disable it line-by-line.

Ignoring all errors in the file

When you place this flag at the very top of your Python file, Pylint ignores that error across the whole file.

# pylint: disable=consider-using-f-string

name = 'world'

a = 'my hello %s' % name

print(a)

b = 'again this name is {}'.format(name)

print(b)

When we re-run the check, Pylint returns:

c0209: formatting a regular string which could be a f-string (consider-using-f-string)

Another alternative is to add this flag to a .pylintrc file. This file should be placed at the root of your project, and by doing so, Pylint will ignore the error across the whole project.

A minimal example in this case would be:

# .pylintrc

[MASTER]

disable=consider-using-f-string

After this change, if we re-run Pylint we get:

c0209: formatting a regular string which could be a f-string (consider-using-f-string)

Ignoring individual errors

To ignore each individual case, place a disabling flag next to the expression #pylint: disable=consider-using-f-stringyou want to ignore.

name = 'world'

a = 'my hello %s' % name #pylint: disable=consider-using-f-string

print(a)

b = 'again this name is {}'.format(name) #pylint: disable=consider-using-f-string

print(b)

The errors will now be suppressed:

c0209: formatting a regular string which could be a f-string (consider-using-f-string)

Conclusion

That's it for today. I hope this article helped you understand and fix the infamous "c0209: formatting a regular string which could be a f-string (consider-using-f-string)" error.

See you next time!