Flake8: f-string is missing placeholders [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
2 min

banner

# Flake8: f-string is missing placeholders [Solved]

The article addresses the following 2 warnings:

  • Flake8: f-string is missing placeholders
  • Using an f-string that does not have any interpolated variables. pylint(f-string-without-interpolation)

The Flake8 error "f-string is missing placeholders" occurs when you have a formatted string literal that doesn't contain any placeholders using curly braces.

To solve the error, either remove the f prefix to declare a normal string or use placeholders in the f-string.

Here is an example of when the error occurs.

main.py
# โ›”๏ธ Flake8: f-string is missing placeholders # Using an f-string that does not have any interpolated variables. pylint(f-string-without-interpolation) a_str = f'bobby hadz . com' print(a_str)

f string is missing placeholders

We have a formatted string literal that doesn't contain any placeholders {} which causes the issue.

One way to resolve this is to remove the f prefix and declare a basic Python string literal.

main.py
a_str = 'bobby hadz . com' print(a_str)

Removing the f prefix resolves the issue because we are no longer declaring a formatted string literal.

Alternatively, you can use placeholders to interpolate variables in the string.

main.py
first = 'bobby' last = 'hadz' a_str = f'{first} {last} . com' print(a_str) # ๐Ÿ‘‰๏ธ bobby hadz . com

The curly braces syntax enables us to interpolate variables in the string.

To use placeholders, you have to prefix the string with f to mark it as a formatted string literal.

You can also use expressions between the curly braces.

main.py
num1 = 10 num2 = 20 a_str = f'The sum is {num1 + num2}' print(a_str) # ๐Ÿ‘‰๏ธ The sum is 30

The code between the curly braces gets evaluated and then gets inserted into the string at the specified position.

You can also use an inline if/else statement if you need to check for a condition in your placeholders.

main.py
num = 100 a_str = f'The result is {500 if num > 0 else 1000}' print(a_str) # ๐Ÿ‘‰๏ธ The result is 500

The if statement checks if the num variable stores a value greater than 0.

If the condition is met, the number 500 is returned, otherwise, the number 1000 is returned.

In short, if you don't have to interpolate variables in your string, you don't have to use an f string.

The following is unnecessary:

main.py
# โ›”๏ธ Flake8: f-string is missing placeholders # Using an f-string that does not have any interpolated variables. pylint(f-string-without-interpolation) a_str = f'bobby hadz . com' print(a_str)

You can simply remove the f prefix in the example to resolve the error.

main.py
a_str = 'bobby hadz . com' print(a_str) # ๐Ÿ‘‰๏ธ bobby hadz . com

You only have to use an f-string if you need to interpolate variables or use expressions in your string literals.

In which case, you can prefix the string with f and interpolate variables using curly braces.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev