Is this supposed to work in Python 3.4:
>>> a='tttt'
>>> print(f'The value of a is {a}')
Is this supposed to work in Python 3.4:
>>> a='tttt'
>>> print(f'The value of a is {a}')
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
No, f
strings were introduced in Python 3.6 which is currently (as of August 2016) in alpha.
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
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.
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.
The line "# -*- coding: future_fstrings -*-" must be the first line of the script.
The hash mark # is important, otherwise this syntax won't work.
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