59

I tried this code, following along with a tutorial:

my_name = 'Zed A. Shaw'

print(f"Let's talk about {my_name}.")

But I get an error message highlighting the last line, like so:

    print(f"Let's talk about {my_name}.")
                                       ^
SyntaxError: invalid syntax

Why?


If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.

In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.

If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.

This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters characters in a string while using .format?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How can I use newline '\n' in an f-string to format a list of strings?, How to fix Unterminated expression in f-string; missing close brace in python?.

For a general overview of the f-string feature, see String with 'f' prefix in python-3.6.

For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.

1
  • "version 3.6 or later" refers to the version of the Python specification, not any specific implementation. I think CPython version numbers happen to mirror the specification versions, but they're not the same thing. Commented May 21 at 19:31

5 Answers 5

76

I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here

Sign up to request clarification or add additional context in comments.

2 Comments

I have python 3.6 installed and still get this error. Some program must still be running python 3.5 which I also have installed. But I'm not sure why a program would run the wrong version of python in the first place.
I saw same error with python 3.8. It was due to the following reason: I had default virtual environment in py 2.7. My script should have used the following command on windows. d:\<my .venv path>\Scripts\Activate.bat && python.exe myscript.py. But I had missed 'python.exe' from the command. It appears to have take default env since I didn't specify python.
16

This is a python version problem.

Instead of using

print(f"Let's talk about {my_name}."

use

print("Let's talk about {}.".format(my_name))

in python2.

Your code works on python3.7.

Check it out here:

my_name= "raushan"
print(f"Let's talk about {my_name}.")

https://repl.it/languages/python3

Comments

5

Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3. 

1 Comment

NIt: not just any version of Python 3 is sufficient, though even when this answer was posted, even Python 3.5 (the last version that did not support f-strings) was very close to EOL.
5
+125

The f-string-syntax, also known as "Literal String Formatting" was proposed as PEP-498 and became part of python with Python-Version 3.6. If you are using a version that does not support this syntax, you will get this syntax error. What does "Python-Version 3.6" mean?

In many languages (C, SQL, JAVA, C++ are popular examples) there is an ISO/ANSI reference document describing what the language should do (often called "the standard") and there are implementations (gcc, clang for C; mysql, postgres for SQL; ...). Both the language standard and the implementations are versioned, but implementations usually reference the specification to declare how much they conform to what version of the standard.

In Python, there are multiple implementations, but there is no such standard. There is however, a "reference implementation", which is CPython, and there is the Python Language Reference. CPython is also the most widely-used implementation of Python. These are the de-facto standard of python. Version numbers of the Python Language Reference and CPython are the same.

So to find out where f-strings are available we can ask: which implementations of python claim to have implemented PEP-498 or claim to fully support Python>=3.6?

  • Jython ❌: Jython only supports Python2.7
  • IronPython ❌: IronPython supports Python2.7 and Python3.4
  • Cython ✅: Supports f-strings since v0.24 (2016) with corrections in v0.26 (2017) changelog
  • MicroPython ✅: MicroPython supports Python3.4 and 'select features from newer versions', and f-strings are part of these selected features. See MicroPython differences from CPython / Python3.6 and it has done that since at least v1.20, I can't find conclusive info on older versions.
  • PyPy ✅: Supports Python 3.11
  • nuitka ✅: Supports Python 3.13

This list currently excludes python-implementations that are old and unmaintained (eg PyJs) as well as python-implementations that are just distributions of CPython (eg ActivePython), mini-languages that live inside python (eg numba) and extensions of python (eg coconut). Feel free to edit if an important implementation is missing.

Comments

1

The other answers are correct that Python 3.6 or later is needed for f-strings to work in standard Python. If for some reason you don't want to upgrade, though, you can run pip install future-fstrings which will allow fstrings to work on earlier versions.

If you are running Python in a different environment, here's how f-strings work in various Python implementations:

Running pip install future-fstrings might let f-strings work on the environments which don't support f-strings, but I'm not sure.

2 Comments

Cython is a tool for integrating Python and C code; CPython simply means the reference implementation (where version 3.6 or later is needed). The sources you found about Cython were talking about how CPython had evolved; as far as I can tell from that, Cython didn't actually have to change to support them... ? At any rate, Cython isn't really an "implementation of Python" so I don't think it belongs in a list like this.
@KarlKnechtel Seems fair.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.