An f-string is a Python string literal that puts variables or code directly inside curly-brace placeholders. Python evaluates each expression and inserts its value into the resulting string. section 2.1.12 section 2.5
A readable formatted phrase
With article = 'an' and word = 'octopus', print(f'Ahoy, Captain, {article} {word} off the larboard bow!') produces: Ahoy, Captain, an octopus off the larboard bow! Compared with str.format(), the variables stay beside their placeholders, so the expression is easier to read. section 2.1.12 section 2.3.4
Use f-strings to:
- Build output from values chosen by a program, such as an article selected for a word. section 2.1.13
- Control displayed widths with printf-style formatting options, such as {lines:8}. section 6.3.2 section 6.5
- Combine computed expressions with fixed text, such as joined adjectives and a randomly selected noun. section 9.3.6
>>> adjs = ', '.join(random.sample(adjectives, k=3))
>>> print(f'You {adjs} {random.choice(nouns)}!')
You heedless, thin-faced, gross recreant!
A runnable example combines a joined list of adjectives and a randomly selected noun inside an f-string. section 9.3.6



