Future F-strings

1.2.0 · deprecated · verified Mon Apr 13

Future F-strings is a library that backported f-string (formatted string literal) syntax to Python versions prior to 3.6. It enables developers to use f-strings in Python 2.7, and Python 3.3-3.5 environments by transforming the source code. The current version is 1.2.0, but the library is now deprecated as its target Python versions have reached end-of-life and MicroPython, a common target for its `rewrite` functionality, now supports f-strings natively.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates two ways to use `future-fstrings`. The first shows how to enable f-strings at runtime in a Python < 3.6 environment using the special encoding cookie at the top of the file. The second explains how to use the `future-fstrings-show` command-line utility to transform the source code into Python 2/3.5 compatible format calls, which is useful for distribution to environments without `future-fstrings` installed.

# -*- coding: future_fstrings -*-

import os

name = os.environ.get('USER_NAME', 'world')
message = f'Hello {name}!'
print(message)

# To demonstrate the CLI transformation (requires future-fstrings[rewrite])
# Save the above code as 'my_app.py'
# Run in your shell:
# future-fstrings-show my_app.py
# This will output the Python2/3.5 compatible code (e.g., using .format())

view raw JSON →