49

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 in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output 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)?.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Alexander Coffman
  • 503
  • 1
  • 5
  • 8

6 Answers6

69

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

rawwar
  • 4,834
  • 9
  • 32
  • 57
  • 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. – personal_cloud Dec 26 '22 at 00:31
  • 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:\\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. – Ishwara Bhat Dec 27 '22 at 07:44
15

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

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Raushan Kumar
  • 324
  • 2
  • 5
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. 

Balaji.J.B
  • 618
  • 7
  • 14
  • 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. – chepner Mar 31 '23 at 12:46
4

f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.

If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
-2

I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.

  • 1
    Please mention the version no you are using and match with the OP's one. Maybe the fix happened later. In case your own code is a bit different, post that as well – NitinSingh Jul 12 '18 at 10:54
  • i use a online compiler for practicing.. i used the same code and the result is Let's talk about Zed A. Shaw. He's 74 inches tall. He's 180 pounds heavy. Actually that's not too heavy. He's got Blue eyes and Brown hair. His teeth are usually White depending on the coffee. – Nagaraju AWS Jul 13 '18 at 12:51
-2

I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.

I had this problem so hopefully, this answer can be of help to those looking for it.