Search code examples
pythonstringpython-3.xf-string

Are f-strings supposed to work in Python 3.4?


Is this supposed to work in Python 3.4:

>>> a='tttt'

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


Solution

  • You could at least emulate them if really needed

    def f(string):
        # (!) Using globals is bad bad bad
        return string.format(**globals())
    
    # Use as follows:
    ans = 'SPAM'
    print(f('we love {ans}'))
    

    Or maybe some other ways, like a class with reloaded __getitem__ if you like f[...] syntax