liveBook · Manning

F-string

As explained in Python Workout: 50 ten-minute exercises

A Python string literal prefixed with f that evaluates embedded expressions and supports format specifications.

An f-string is a Python string literal prefixed with f that evaluates expressions inside curly braces. It combines text and computed values without separately passing formatting parameters. section 1.6

Use f-strings when output needs both readable text and values such as numbers, names, or expression results. Expressions can include arithmetic and method calls, while format specifications control presentation. section 1.6 section 1.13

Formatting an imprecise float

>>> s = 0.1 + 0.7 >>> print(s) 0.7999999999999999 >>> print(f'{s:.2f}') 0.80 The f-string controls displayed precision; it does not make floating-point arithmetic exact. Use integers instead of floats when sensitive measurements require that choice. section 1.13

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains interpolation, expressions, alignment, and numeric precision, while deferring the full formatting-code reference.
Examples
Uses greetings, computed expressions, aligned names, and a floating-point value displayed with two decimal places.
What it assumes
Assumes variables, expressions, strings, and floating-point values; compares against percent formatting and str.format.
What you can do after
Apply it via Python to produce mixed-value output and control numeric or textual presentation.
How it connects
Connects string formatting with the percent operator, str.format, floating-point numbers, and method calls.

Also explained in

Compare how 4 books explain f-string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.