Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}')

like image 812
Quora Feans Avatar asked Aug 09 '16 23:08

Quora Feans


3 Answers

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(f'hello {thing}')

Runtime:

$ python2.7 main.py
hello world
like image 82
Anthony Sottile Avatar answered Oct 21 '22 01:10

Anthony Sottile


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

like image 25
alecxe Avatar answered Oct 21 '22 03:10

alecxe


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

like image 3
thodnev Avatar answered Oct 21 '22 02:10

thodnev