f-strings giving SyntaxError?

88,700

Solution 1

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

Solution 2

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

Solution 3

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. 

Solution 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.

Share:
88,700
Admin
Author by

Admin

Updated on April 09, 2021

Comments

  • Admin
    Admin about 3 years

    I am getting an error message with my Atom reader here, where it is suggesting the first print.(f"message") is delivering an error:

    File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75
        print(f"Let's talk about {my_name}.")
                                           ^
    SyntaxError: invalid syntax
    [Finished in 0.077s]
    

    Code:

    my_name = 'Zed A. Shaw'
    my_age = 35 # not a lie
    my_height = 74 # inches
    my_weight = 180 #lbs
    my_eyes = 'Blue'
    my_teeth = 'White'
    my_hair = 'Brown'
    
    print(f"Let's talk about {my_name}.")
    print(f"He's {my_height} inches tall.")
    print(f"He's {my_weight} pounds heavy.")
    print("Actually that's not too heavy.")
    print(f"He's got {my_eyes} eyes and {my_hair} hair.")
    print(f"His teeth are usually {my_teeth} depending on the coffee.")