f-strings
---
id: f-strings
title: How to Format Strings with f-Strings
description: How to insert variable and expression values directly into strings using f-strings in Python
tags:
- f-strings
- string formatting
- Python
- programming
sidebar_position: 13
isPublic: false
---
# How to Format Strings with f-Strings
`f-strings` offer a straightforward and readable way to incorporate variable values or expressions into strings in Python.
They are used by prefixing the string with `f` or `F`, and variables or expressions can be inserted within curly braces `{}`.
```python title="Example of using f-strings"
name = "CodeBuddy"
age = 20
greeting = f"My name is {name}, and I am {age} years old."
# "My name is CodeBuddy, and I am 20 years old."
print(greeting)
In this example, the f-string
is used to insert the values of the variables name
and age
into the string greeting
.
Applying Expressions in f-Strings
Within an f-string, you can include expressions such as arithmetic operations, comparisons, and more.
An expression refers to a combination of variables or values that computes to a result.
Example of using an exponentiation expression
number = 10
result = f"The square of 10 is {number**2}."
print(result) # "The square of 10 is 100."
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.