f-strings giving SyntaxError?

我的梦境 提交于 2019-12-17 06:51:07

问题


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

回答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




回答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.




回答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.




回答4:


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 checkout here-

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

here

it works



来源:https://stackoverflow.com/questions/50401632/f-strings-giving-syntaxerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!