Codectionary / Developer documentation / Python

f-Strings

f-strings (formatted string literals) are Python's modern, preferred way to embed expressions inside string literals. Prefixing a string with f lets you write variables and expressions directly inside curly braces, and Python evaluates and inserts them at runtime. f-strings also support format specifiers for controlling decimal places, padding, and alignment, making them more readable and faster than older formatting methods like %-formatting or .format().

Syntax

f"text {expression}"

Examples

Basic Interpolation

Embedding variable values directly inside a string.

name = "Fola"
age = 21
print(f"{name} is {age} years old")

Expressions Inside f-strings

f-strings can contain any valid expression, not just plain variable names.

a = 5
b = 3
print(f"{a} + {b} = {a + b}")

items = ["apple", "banana", "cherry"]
print(f"You have {len(items)} items")

user = {"name": "Priya"}
print(f"Hello, {user['name']}!")

Formatting Numbers

Using format specifiers to control decimal places, padding, and thousands separators.

price = 19.5
print(f"${price:.2f}")        # $19.50

number = 7
print(f"{number:03d}")        # 007

population = 1500000
print(f"{population:,}")      # 1,500,000

Multi-line f-strings

Combining f-strings with triple quotes for readable multi-line output.

name = "Fola"
role = "Developer"

profile = f"""
Name: {name}
Role: {role}
Status: Active
"""
print(profile)

Best practices

  • Prefer f-strings over %-formatting or .format() in modern Python - they are more readable and generally faster
  • Use format specifiers like :.2f for currency and :, for thousands separators instead of manual string manipulation
  • Keep expressions inside f-strings simple - if logic gets complex, compute the value in a variable first
  • Remember f-strings require Python 3.6 or later - check your target Python version if compatibility matters

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.