Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested f-strings

Thanks to David Beazley's tweet, I've recently found out that the new Python 3.6 f-strings can also be nested:

>>> price = 478.23 >>> f"{f'${price:0.2f}':*>20s}" '*************$478.23' 

Or:

>>> x = 42 >>> f'''-{f"""*{f"+{f'.{x}.'}+"}*"""}-''' '-*+.42.+*-' 

While I am surprised that this is possible, I am missing on how practical is that, when would nesting f-strings be useful? What use cases can this cover?

Note: The PEP itself does not mention nesting f-strings, but there is a specific test case.

like image 657
alecxe Avatar asked Dec 19 '16 03:12

alecxe


People also ask

Can you nest F-strings in Python?

Nested F-StringsYou can embed f-strings inside f-strings for tricky formatting problems like adding a dollar sign to a right aligned float, as shown above.

How do you concatenate F-strings?

If you have to concatenate sequence of strings with a delimited, then use join() function. If some formatting is also required with concatenation, then use format() function or f-string. Note that f-string can be used with Python 3.6 or above versions.

What are F-strings in Python?

“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.

What is nested string?

Nesting or String searches utilize parentheses to clarifiy relationships between terms and to specify the order the search should be run. The keywords inside the parentheses are required to be searched first and generally those items are linked by the Boolean Operator "OR."


2 Answers

I don't think formatted string literals allowing nesting (by nesting, I take it to mean f'{f".."}') is a result of careful consideration of possible use cases, I'm more convinced it's just allowed in order for them to conform with their specification.

The specification states that they support full Python expressions* inside brackets. It's also stated that a formatted string literal is really just an expression that is evaluated at run-time (See here, and here). As a result, it only makes sense to allow a formatted string literal as the expression inside another formatted string literal, forbidding it would negate the full support for Python expressions.

The fact that you can't find use cases mentioned in the docs (and only find test cases in the test suite) is because this is probably a nice (side) effect of the implementation and not it's motivating use-case.


Actually, with two exceptions: An empty expression is not allowed, and a lambda expression must be surrounded by explicit parentheses.

like image 131
Dimitris Fasarakis Hilliard Avatar answered Oct 12 '22 18:10

Dimitris Fasarakis Hilliard


I guess this is to pass formatting parameters in the same line and thus simplify f-strings usage.

For example:

>>> import decimal >>> width = 10 >>> precision = 4 >>> value = decimal.Decimal("12.34567") >>> f"result: {value:{width}.{precision}}" 'result:      12.35' 

Of course, it allows programmers to write absolutely unreadable code, but that's not the purpose :)

like image 38
Eugene Lisitsky Avatar answered Oct 12 '22 18:10

Eugene Lisitsky