Python f-strings 给出 SyntaxError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50401632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
f-strings giving SyntaxError?
提问by Alexander Coffman
I am getting an error message with my Atom reader here, where it is suggesting the first print.(f"message")
is delivering an error:
我的 Atom 阅读器在这里收到一条错误消息,它表明第一个print.(f"message")
是传递错误:
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.")
回答by InAFlash
回答by Raushan Kumar
This is a python version problem.
这是python版本问题。
Instead of using
而不是使用
print(f"Let's talk about {my_name}."
use
用
print("Let's talk about {}.".format(my_name))
in python2.
在python2中。
Your code works on python3.7.
您的代码适用于 python3.7。
Check it out here:
在这里查看:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
回答by Balaji.J.B
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.?
Python 解释器会导致以下问题,因为您在执行程序时调用了错误的 Python 版本,因为 f 字符串是 Python 3 而不是 Python 2 的一部分。?你可以这样做python3 filename.py
,它应该可以工作。要解决此问题,请将 python 解释器从 2 更改为 3。?
回答by Josh Turner
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.
我相信您在这里遇到的问题在于您在没有意识到的情况下使用 python 2。如果你还没有在你的机器上将它设置为将 python 3 作为你的默认版本,你应该在终端中执行 python3 而不是标准的“python”命令。
I had this problem so hopefully, this answer can be of help to those looking for it.
我遇到了这个问题,所以希望这个答案可以帮助那些寻找它的人。
回答by Nagaraju AWS
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.
我认为这是由于旧版本。我已经尝试过新版本并且执行得很好。结果正如预期的那样。
回答by Aran-Fey
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
在 python 3.6中添加了f 字符串。在较旧的 Python 版本中,f 字符串将导致语法错误。
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.
如果您不想(或不能)升级,请参阅如何将变量放入 Python 中的字符串中?对于 f-strings 的替代品。