8

Is this supposed to work in Python 3.4:

>>> a='tttt'

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

wim
  • 338,267
  • 99
  • 616
  • 750
Quora Feans
  • 928
  • 3
  • 10
  • 21
  • Good question. I am wondering if this might be included in Python 3.4 with a kind of `from __future__ import fstrings` syntax? – elzell Oct 05 '16 at 13:41
  • I too would like to know... if not, we can't use them in public packages unless we discard Debian Stable users.. –  Nov 05 '16 at 19:44

5 Answers5

15

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
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
11

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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
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

thodnev
  • 1,564
  • 16
  • 20
3

I just wrote a back-port compiler for f-string, called f2format. You may write f-string literals in Python 3.6 flavour, and compile to a compatible version for end-users to run, just like Babel for JavaScript.

f2format shall replace f-string literals with str.format methods, whilst maintaining the original layout of source code. You can simply use

f2format /path/to/the/file_or_directory

which will rewrite all Python files in place. For instance,

# original source code
var = f'foo{(1+2)*3:>5}bar{"a", "b"!r}boo'
# after f2format
var = ('foo{:>5}bar{!r}boo').format(((1+2)*3), ("a", "b"))

String concatenation, conversion, format specification, multi-lines and unicodes are all treated right. For more information, just check out the README.

Jarry Shaw
  • 81
  • 6
0

Yes, it is possible to use f-strings with Python 3.x lower than 3.7 if you install and use the future-fstrings module, but there are important nuances.

  1. The line "# -*- coding: future_fstrings -*-" must be the first line of the script.

  2. The hash mark # is important, otherwise this syntax won't work.

  3. The version of future-fstrings and tokenize_rt modules must be appropriate, otherwise you get strange error messages. I will post here more details about this problem, because I saw many people googling these error messages.

Simple scripts:

test_fstrings_A.py:
import test_fstrings_B

test_fstrings_B.py:
# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')

Output:

$ python3 test_fstrings_B.py
  File "test_fstrings_B.py", line 1
SyntaxError: encoding problem: future_fstrings

$ python3 test_fstrings_A.py
Traceback (most recent call last):
  File "test_fstrings_A.py", line 2, in <module>
    import test_fstrings_B
  File "/home/build01/test_fstrings_B.py", line 0
SyntaxError: invalid syntax (tokenize_rt.py, line 22)

I had this trouble, because I had wrong versions of the mentioned modules on my system:

$ cd $HOME/.local/lib/python3.5/site-packages; ls -d *.dist-info; cd -
future_fstrings-1.2.0.dist-info  pip-20.3.1.dist-info  tokenize_rt-4.0.0.dist-info

In order to check if the versions are correct one can first install the module in a local directory as a .whl and test it this way as I learned from this issue https://bugs.python.org/issue33944#msg333705

$ python3 -m pip download -d pkgs future_fstrings
...
Saved ./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl
Saved ./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl
Successfully downloaded tokenize-rt future-fstrings

$ PYTHONPATH=./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl:./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl python3 test_fstrings_A.py
hello world
$ PYTHONPATH=./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl:./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl python3 test_fstrings_B.py
hello world

In other words, the command "python3 -m pip download" installs and outputs correct (appropriate for the installed Python version) versions of modules. This helped me to see that the version tokenize_rt-4.0.0 is wrong in my Python installation location.

The concluding step was to fix it globally, using --force-reinstall parameter for pip3:

$ pip3 install --force-reinstall pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl
or
$ python3 -m pip install --force-reinstall future_fstrings

UPDATE: In some cases the command "python3 -m pip download" installs anyway the wrong version 4.0.0 of tokenize_rt. In this case only explicit version specification helped:

$ python3 -m pip install --force-reinstall future_fstrings==1.2.0 tokenize_rt==3.2.0
Alexander Samoylov
  • 2,358
  • 2
  • 25
  • 28