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

Some weeks I ago I faced this problem in one of my projects after upgrading [`pylint`](https://pylint.org/) to `2.11`.

The error was:

```console
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](https://github.com/PyCQA/pylint/pull/4796) on Pylint's github page.

It turns out, this is a new feature that [landed on Pylint 2.11.0.](https://github.com/PyCQA/pylint/releases/tag/v2.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](https://miguendes.me/73-examples-to-help-you-master-pythons-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.

```python
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)](https://cdn.hashnode.com/res/hashnode/image/upload/v1636878592489/KbPKGr1aD.png)

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](https://miguendes.me/73-examples-to-help-you-master-pythons-f-strings) to learn the myriad ways you can use a f-string.

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

```python
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)](https://cdn.hashnode.com/res/hashnode/image/upload/v1636878965935/brdWKINwL.png)

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.

```python
# 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)](https://cdn.hashnode.com/res/hashnode/image/upload/v1636879041373/eUKFsrxH5.png)

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)](https://cdn.hashnode.com/res/hashnode/image/upload/v1636879041373/eUKFsrxH5.png)

#### Ignoring individual errors

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

```python
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)](https://cdn.hashnode.com/res/hashnode/image/upload/v1636879041373/eUKFsrxH5.png)

## 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!
