38

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.")
smci
  • 29,564
  • 18
  • 109
  • 144
Alexander Coffman
  • 393
  • 1
  • 4
  • 8
  • 8
    What Python version are you using? f-strings are a relatively recent addition to the language. – jasonharper May 17 '18 at 23:06
  • 2
    You need python3 at least, not python2 – Calumah May 17 '18 at 23:07
  • I'm learning from the "Learn Python the hard way" book and for some reason even though I just installed 3.6 it still says invalid syntax. File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75 print(f"Let's talk about {my_name}.") ^ SyntaxError: invalid syntax [Finished in 0.176s] With the problem being at the ." at the end. – Alexander Coffman May 17 '18 at 23:15
  • 1
    can you show what's being printed when you type python in command prompt – InAFlash May 17 '18 at 23:16
  • I don't know how to get it to show in the command prompt – Alexander Coffman May 17 '18 at 23:17
  • It should say something like `Python 3.5.2 (default, Nov 23 2017, 16:37:01)` when you launch the repl. You can also do `python --version`. – Paul Rooney May 18 '18 at 00:16
  • 2
    On Windows you can type Ctrl-r, then `command` and finally press the `Enter` key. – martineau May 18 '18 at 00:17
  • 2
    You can see what version of Python is being used by adding an `import sys` line, then separately one that's `print(sys.version_info)`. – martineau May 18 '18 at 00:20
  • Just a late note here: if you're running this in windows in an alternate shell (cmder, etc) you might have a python binary in addition to whatever you've installed in windows. Something to consider, perhaps. – Matt Murphy Jun 18 '20 at 17:15

7 Answers7

59

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

InAFlash
  • 4,545
  • 7
  • 32
  • 53
14

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
  • 19,150
  • 23
  • 79
  • 104
Raushan Kumar
  • 314
  • 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
  • 528
  • 6
  • 14
3

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
  • 35,525
  • 9
  • 94
  • 135
0

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
0

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.

0

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