F-strings giving SyntaxError in Python

F-strings giving SyntaxError in Python is an error common in versions of Python prior to Python 3.6, the solution is to let go of your old installation.

Explaining The Error : F-strings giving SyntaxError in Python

I got the error the first time when I ran print.(f”message”)

This is an example of the code that I was running

                                                                       #
my_firstname = 'sarah'
my_lastname = 'ally'
my_age = '28'
my_ethnicity = 'latino'

print(f"This is {my_firstname}.")
print(f"This is {my_lastname}.")
print(f"This is {my_age}.")
print(f"This is {my_ethnicity}.")
                                                                       #

This is how the error looked like

                                                                       #
File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75
    print(f"This is {my_firstname}.")
                                       ^
SyntaxError: invalid syntax
[Finished in 0.077s]
                                                                       #

After some research I figured out that the error was caused because of the Python version, I was running Python 3.5, bellow are the possible solutions to this error, including the solution that worked for me.

Solution 1 : install Python versions above Python 3.6

This is the solution that worked for me and for most developers, remove your old Python installation, in my case it is Python 3.5.

Then install another new version of Python, in my case I installed Python 3.6.

The error disappears, the reason the error is gone is because F-string literals have been added to Python version 3.6 and above.

The error will only occur if you are running Python 3.5 or prior versions.

Solution 2 : correctly avoid using f-string literals

This solution is a quick fix for people who do not want to install and upgrade Python again

To do that, you can replace the code bellow

                                                                       #
print(f"this is {my_firstname}."
                                                                       #

With the code bellow, since the goal is to avoid using the f-string literals.

                                                                       #
print("this is {}.".format(my_firstname))
                                                                       #

Solution 3 : use python3 instead

Other people have this problem because they are running Python 2 without even knowing it.

To run Python3 you should execute python3 on the terminal instead of the standard python.

So you should replace the code bellow

                                                                       #
python myfile.py
                                                                       #

With the following code

                                                                       #
python3 file.py
                                                                       #

If this is the root of your issue then the error should be gone by now.

If this solved your issue congrats, please consider supporting the team by donating to our Kofi account, you do not have to do this, any help is much appreciated.

Summing-up

This is it, it is the end of our article, I hope I helped you solve this Python error, a simple upgrade will usually solve this issue. Do not give up, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/