Quo*_*ans 3 python string python-3.x f-string
这应该适用于Python 3.4:
>>> a='tttt'
>>> print(f'The value of a is {a}')
我写了一个(可怕的)东西,通过编码技巧启用它们:
第一:
pip install future-fstrings
Run Code Online (Sandbox Code Playgroud)
然后替换编码cookie(如果你有)和bam!python中的f字符串<3.6
# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')
Run Code Online (Sandbox Code Playgroud)
运行:
$ python2.7 main.py
hello world
Run Code Online (Sandbox Code Playgroud)
如果确实需要的话,你至少可以模仿他们
def f(string):
# (!) Using globals is bad bad bad
return string.format(**globals())
# Use as follows:
ans = 'SPAM'
print(f('we love {ans}'))
Run Code Online (Sandbox Code Playgroud)
__getitem__或者也许还有其他方法,比如如果您喜欢 f[...] 语法,则重新加载类