f字符串是否适用于Python 3.4?

Quo*_*ans 3 python string python-3.x f-string

这应该适用于Python 3.4:

>>> a='tttt'

>>> print(f'The value of a is {a}')

Ant*_*ile 9

我写了一个(可怕的)东西,通过编码技巧启用它们:

第一:

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)


ale*_*cxe 5

不,f字符串是在Python 3.6中引入的,Python 3.6目前(截至2016年8月)为alpha版本。


tho*_*nev 4

如果确实需要的话,你至少可以模仿他们

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[...] 语法,则重新加载类