f-strings giving SyntaxError?

Question:

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)?.

Asked By: Alexander Coffman

||

Answers:

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

Answered By: rawwar

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.

Answered By: Nagaraju AWS

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.

Answered By: Aran-Fey

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

Answered By: Raushan Kumar

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. 

Answered By: Balaji.J.B

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.

Answered By: Josh Turner

I think they had typed

python file.py

to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type

python3 file.py

That’s the solution for the problem
python2 and python3 running command

Answered By: Vishwanath reddy