invalid syntax error when trying to perform string interpolation in Python

The error, invalid syntax error when trying to perform string interpolation in Python is a popular error that occurs in Python versions 3.5 and bellow

This article is dedicated to solving this error by sharing the solutions that worked for me and for other Python developers.

Explain The Error : invalid syntax error when trying to perform string interpolation in Python

The error is a string interpolation formatting error, it occurs when you can not get the formatting to work.

In my case I got it By running Python 3 on Ubuntu 16.04:

                                                                       #
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.
IPython 5.2.2 -- An enhanced Interactive Python.
                                                                       #
                                                                       #
python3 --version
Python 3.5.2
                                                                       #

As you can see above, I have Python 3 running on my machine and not Python 2, First I thought the error was caused by Python 2, but I had the third version installed

If you want to check your Python version in any operating system or environment, use the code bellow in your terminal of choice.

                                                                       #
python – version
                                                                       #

If you want, you can replicate the error by running the Python code bellow, I got a syntax error at the end. That is what we want to solve.

                                                                       #
In [1]: state = "Alabama"

In [2]: state
Out[2]: 'Alabama'

In [3]: my_message = f"I live in {state}"
File "<ipython-input-3-d004dd9e0255>", line 1
my_message = f"I live in {state}"
                                ^
SyntaxError: invalid syntax
                                                                       #

The confusing thing was that I was running the same code at my other computer and everything worked fine. That was before I figured out how to solve the issue. Bellow is the solution that worked for me plus other possible solutions.

Solution 1 : Upgrade to Python 3.6 or above

It is so simple guys, It may sound silly but this is the solution that worked for me and for most people, just upgrade to Python version 3.6 or above and the error will disappear.

The reason why this solution worked is that this type of string interpolation was not available in Python 3.5, this was added in Python 3.6, it is called “PEP 498: Formatted string literals”.

If this solution worked for you, support the team on Kofi but you do not have to, we are here to help fellow developers for free.

Now if the above solution did not work, you might be in deeper trouble, bellow we added other possible solutions to the problem.

Solution 2 : Upgrade to Python 3.6 or above

If you do not want to upgrade to Python 3.6 this is the solution for you, just add the code bellow and see what happens, this solution works for Python 3.5 as far as I know but you can try it with other versions of Python like Python 3.4

                                                                       #
my_message = "I live in {}".format(state)
                                                                       #

Summing-up

It seems that there are only two possible solutions to the problem, upgrading to Python 3.6 worked for me and for most people, the second solution works for people who want to keep the Python version as it is.

I can not find any other solution to the problem guys, I tried my best, I hope the above solutions worked for you, cheers, keep coding.

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