78

I'm trying out Python 3.6. Going through new code, I stumbled upon this new syntax:

f"My formatting string!"

It seems we can do things like this:

>>> name = "George"
>>> print(f"My cool string is called {name}.")
My cool string is called George.

Can anyone shed some light on the inner workings of this? In particular what is the scope of the variables that an f-prefixed string can take?

0

4 Answers 4

68

See PEP 498 Literal String Interpolation:

The expressions that are extracted from the string are evaluated in the context where the f-string appeared. This means the expression has full access to local and global variables. Any valid Python expression can be used, including function and method calls.

So the expressions are evaluated as if they appear in the same scope; locals, closures, and globals all work the same as in other code in the same context.

You'll find more details in the reference documentation:

Expressions in formatted string literals are treated like regular Python expressions surrounded by parentheses, with a few exceptions. An empty expression is not allowed, and a lambda expression must be surrounded by explicit parentheses. Replacement expressions can contain line breaks (e.g. in triple-quoted strings), but they cannot contain comments. Each expression is evaluated in the context where the formatted string literal appears, in order from left to right.

Since you are trying out a 3.6 alpha build, please do read the What's New In Python 3.6 documentation. It summarises all changes, including links to the relevant documentation and PEPs.

And just to be clear: 3.6 isn't released yet; the first alpha is not expected to be released until May 2016. See the 3.6 release schedule.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it expected that f-strings will be backported to previous versions of python3 (if they find heavy usage in new code)? I'm not able to find an answer on this either way, but this seems like a point of contention.
There are no plans to backport this, no. The PEP states this is a new feature for 3.6.
Thanks. I was trying to compare it to the Enum PEP which was new but eventually backported to previous 3.x versions, and I didn't see anything indicating a plan either way.
@ChrisArena: The Enum feature is pure code; it can be backported to previous versions of Python as an installable add-on, so older Python versions don't require a new release. f strings are syntax, a change to how Python itself works. You can't backport syntax as an add-on.
@JürgenA.Erhard: of course, it supports any valid expression.
16

f-strings also support any Python expressions inside the curly braces.

print(f"My cool string is called {name.upper()}.")

Comments

10

It might also be worth noting that this PEP498 has a backport to Python <3.6

pip install fstring

from fstring import fstring

x = 1

y = 2.0

plus_result = "3.0"

print fstring("{x}+{y}={plus_result}")

# Prints: 1+2.0=3.0

Comments

0

letter f for "format" as in f"hello {somevar}. This little f before the "(double-quote) and the {} characters tell python 3, "hey, this string needs to be formatted. So put these variable in there and format it.".

hope this is clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.