Are f-strings supposed to work in Python 3.4?

Are f-strings supposed to work in Python 3.4?

I wrote a (terrible) thing that enables them via an encoding trick:

First:

pip install future-fstrings

and then replace the encoding cookie (if you have one) and bam! f-strings in python<3.6

# -*- coding: future_fstrings -*-
thing = world
print(fhello {thing})

Runtime:

$ python2.7 main.py
hello world

No, f strings were introduced in Python 3.6 which is currently (as of August 2016) in alpha.

Are f-strings supposed to work in Python 3.4?

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

Leave a Reply

Your email address will not be published. Required fields are marked *