python-3.6中带有'f'前缀的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35745050/
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
String with 'f' prefix in python-3.6
提问by DevShark
I'm trying out Python 3.6. Going through new code, I stumbled upon this new syntax:
我正在尝试 Python 3.6。通过新代码,我偶然发现了这个新语法:
f"My formatting string!"
It seems we can do things like this:
似乎我们可以做这样的事情:
>>> name = "George"
>>> print(f"My cool string is called {name}.")
My cool string is called George.
Can anyone shed some light on the inner workings of this? In particular what is the scope of the variables that an f-prefixed string can take?
任何人都可以对它的内部运作有所了解吗?特别是 f 前缀字符串可以采用的变量范围是什么?
采纳答案by Martijn Pieters
See PEP 498 Literal String Interpolation:
请参阅PEP 498文字字符串插值:
The expressions that are extracted from the string are evaluated in the context where the f-string appeared. This means the expression has full access to local and global variables. Any valid Python expression can be used, including function and method calls.
从字符串中提取的表达式在 f 字符串出现的上下文中进行计算。这意味着表达式可以完全访问局部和全局变量。可以使用任何有效的 Python 表达式,包括函数和方法调用。
So the expressions are evaluated as if they appear in the same scope; locals, closures, and globals all work the same as in other code in the same context.
因此,表达式的计算就像它们出现在同一范围内一样;局部变量、闭包和全局变量的工作方式与同一上下文中的其他代码相同。
You'll find more details in the reference documentation:
您可以在参考文档中找到更多详细信息:
Expressions in formatted string literals are treated like regular Python expressions surrounded by parentheses, with a few exceptions. An empty expression is not allowed, and a
lambda
expression must be surrounded by explicit parentheses. Replacement expressions can contain line breaks (e.g. in triple-quoted strings), but they cannot contain comments. Each expression is evaluated in the context where the formatted string literal appears, in order from left to right.
格式化字符串文字中的表达式被视为用括号括起来的正则 Python 表达式,但有一些例外。不允许使用空表达式,并且
lambda
表达式必须用显式括号括起来。替换表达式可以包含换行符(例如在三引号字符串中),但它们不能包含注释。每个表达式都在出现格式化字符串文字的上下文中按从左到右的顺序进行计算。
Since you are trying out a 3.6 alpha build, please do read the What's New In Python 3.6documentation. It summarises all changes, including links to the relevant documentation and PEPs.
由于您正在尝试 3.6 alpha 版本,请务必阅读Python 3.6文档中的新增功能。它总结了所有更改,包括指向相关文档和 PEP 的链接。
And just to be clear: 3.6 isn't released yet; the first alpha is not expected to be released until May 2016. See the 3.6 release schedule.
而仅仅是明确的:3.6没有被释放尚未; 第一个 alpha 预计要到 2016 年 5 月才会发布。请参阅3.6 发布时间表。
回答by David Gladson
f-strings also support any Python expressions inside the curly braces.
f-strings 还支持大括号内的任何 Python 表达式。
print(f"My cool string is called {name.upper()}.")
回答by RinSlow
It might also be worth noting that this PEP498 has a backport to Python <3.6
可能还值得注意的是,此 PEP498 具有对 Python <3.6 的反向移植
pip install fstring
pip install fstring
from fstring import fstring
x = 1
y = 2.0
plus_result = "3.0"
print fstring("{x}+{y}={plus_result}")
# Prints: 1+2.0=3.0
回答by Kwong Leong
letter f for "format" as in f"hello {somevar}. This little f before the "(double-quote) and the {} characters tell python 3, "hey, this string needs to be formatted. So put these variable in there and format it.".
字母 f 表示“格式”,如 f"hello {somevar}。"(双引号)和 {} 字符之前的这个小 f 告诉 python 3,“嘿,这个字符串需要格式化。所以把这些变量放在在那里格式化。”。
hope this is clear.
希望这很清楚。
回答by red fred
i had issue like this, in the line of imported module:
response_json = requests.get(f'{self.api_url}/{path}', params).json()
i fixed it like this:
我在导入模块的行中遇到了这样的问题:
response_json = requests.get(f'{self.api_url}/{path}', params).json()
我是这样修复的:
response_json = requests.get('{}/{}'.format(self.api_url, path), params).json()