Python f-string

A Python f-string (formatted string literal) allows you to insert variables or expressions directly into a string by placing them inside curly braces {}.

This method makes your code more readable and is often faster than other string formatting techniques.

Example

name = "Jennifer"
age = 23

# Use f-string to embed the name and age variables in a string message = f"My name is {name} and I am {age} years old."
print(message) # Output: My name is Jennifer and I am 23 years old.

Syntax of Python f-string

The f-string starts with f or F followed by quotation marks. Place any variables or expressions you want to embed within curly braces {} inside the string.

For example,

f"string with {expression}"

Here,

  • f or F: Prefix the string with f or F to designate it as an f-string.
  • " ": Use either double or single quotes around the string.
  • {expression}: Place any variable, calculation, or expression inside curly braces {} to embed it within the string.

Example: Python f-string

language = "Python"

# Use f-string to embed the language variable in a string
text = f"Learn {language} with 'ProgramizPRO'."

print(text)

Output

Learn Python with 'ProgramizPRO'.

In the above example,

  • f"Learn {language} with 'ProgramizPRO'." is the f-string.
  • {language} is a placeholder for the language variable.

Evaluate Expression Inside f-string

You can perform calculations directly within the f-string. For example,

num1 = 5
num2 = 4

# Evaluate the sum of num1 and num2 inside f-string result = f"Sum of {num1} and {num2} is {num1 + num2}."
print(result)

Output

Sum of 5 and 4 is 9.

Here, {num1 + num2} is an expression evaluated inside the f-string.


Python f-string Placeholder

You can place various expressions inside the placeholder of an f-string. For example,

Calling Functions Inside Placeholders

You can call a function directly within an f-string placeholder, and the return value will be embedded in the string.

def greeting():
    return "Good morning"

# Call greeting() inside f-string text = f"{greeting()}, everyone!"
print(text) # Output: Good morning, everyone!

Here, the function greeting() returns "Good morning", which is then placed directly into the f-string using {greeting()}.

Using Conditional Expressions Inside Placeholders

You can use conditional (if-else) expressions within an f-string placeholder to dynamically adjust the output based on variable values.

age = 18

# Using a conditional expression inside the f-string text = f"You are {'an adult' if age >= 18 else 'a minor'}."
print(text) # Output: You are an adult.

In this example:

  • { 'an adult' if age >= 18 else 'a minor' } evaluates the condition.
  • If age is 18 or older, it outputs "an adult"; otherwise, it outputs "a minor".

Access Dictionary Values Inside Placeholder

You can also access dictionary values directly within f-strings by using the dictionary key inside the curly braces {}.

person = {"country": "USA", "employee_ID": 121}

# Accessing dictionary values inside the f-string text = f"Country: {person['country']}, Employee ID: {person['employee_ID']}"
print(text) # Output: Country: USA, Employee ID: 121

In the above example, we have accessed the values of the person dictionary inside the f-string.

Here,

  • {person['country']} is a placeholder that accesses the value of the country key.
  • {person['employee_ID']} is a placeholder that accesses the value of the employee_ID key.

Tips for Using f-Strings

How should I handle quotes inside an f-string?

If you use double quotes for the f-string, use single quotes inside it, and vice versa. Otherwise, it will throw a syntax error. That is,

Valid way

# Double quotes outside, single quotes inside
text = f"Learn {language} with 'ProgramizPRO'." 

# Single quotes outside, double quotes inside
text = f'Learn {language} with "ProgramizPRO".'  

Invalid way

# Causes a syntax error due to matching quotes inside and outside
text = f'Learn {language} with 'ProgramizPRO'.'
text = f"Learn {language} with "ProgramizPRO"."
Why use f-strings?

Here are some of the reasons why f-strings are widely used:

Readable: f-strings make code cleaner and more readable by embedding variables directly in strings.

Fast: They’re faster than older methods like .format() as they’re evaluated at runtime.

Flexible: f-strings allow expressions and function calls within the string.

In short, f-strings are a fast, readable, and versatile way to format strings.

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community