SyntaxStudy
Sign Up

f-strings

function Python 3.6

Formatted string literals — embed expressions inside string constants using curly braces.

Syntax

f"text {expression:.format_spec}"

Example

python
name  = 'Alice'
age   = 28
price = 9.99

print(f'Hello, {name}!')
print(f'Age: {age}, Next year: {age + 1}')
print(f'Price: ${price:.2f}')   # Price: $9.99
print(f'{name!r}')             # 'Alice' (repr)
print(f'PI: {3.14159:.4f}')   # PI: 3.1416

# Dict access
user = {'name': 'Bob'}
print(f"Hello {user['name']}")