Open In App

f-strings in Python

Last Updated : 07 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python introduced f-strings (formatted string literals) in version 3.6 to make string formatting and interpolation easier. With f-strings, you can directly embed variables and expressions inside strings using curly braces {}.

How to use f-strings

Simply prefix a string with f and place variables or expressions inside {}. This works like str.format(), but in a more concise and readable way.

Example – Printing variables with f-strings

Python
val = 'Geeks'
print(f"{val}for{val} is a portal for {val}.")


name = 'Om'
age = 22
print(f"Hello, My name is {name} and I'm {age} years old.")

Output
GeeksforGeeks is a portal for Geeks.
Hello, My name is Om and I'm 22 years old.
  • Import the datetime module.
  • Get today’s date using datetime.date.today().
  • Use an f-string to format and print the date.

Formatting codes:

  • %B → full month name
  • %d → day of the month
  • %Y → year
Python
import datetime

today = datetime.datetime.today()
print(f"{today:%B %d, %Y}")

Output
September 07, 2025

Note: F-strings are faster than the two most commonly used string formatting mechanisms, which are % formatting and str.format(). 

Quotation Marks in f-string in Python

  • In f-strings, you can use single (') or double (") quotes.
  • The quotes inside the expression must differ from the quotes used to define the f-string.
  • If both are the same, Python will throw a syntax error.
Python
print(f"'GeeksforGeeks'")

print(f"""Geeks"for"Geeks""")

print(f'''Geeks'for'Geeks''')

Output
'GeeksforGeeks'
Geeks"for"Geeks
Geeks'for'Geeks

Evaluate Expressions with f-Strings in Python

  • f-strings can also evaluate expressions, not just variables.
  • Simply place the expression inside {} within the f-string.
  • Python will evaluate the expression and print the result.
Python
english = 78
maths = 56
hindi = 85

print(f"Ram got total marks {english + maths + hindi} out of 300")

Output
Ram got total marks 219 out of 300

Errors while using f-string in Python

Backslashes in f-string in Python

In Python f-string, Backslash Cannot be used in format string directly.

Python
f"newline: {ord('\n')"

Output

Hangup (SIGHUP)
  File "Solution.py", line 1
    f"newline: {ord('\n')"
    ^
SyntaxError: f-string expression part cannot include a backslash

However, we can put the backslash into a variable as a workaround though :

Python
newline = ord('\n')

print(f"newline: {newline}")

Output
newline: 10

Inline comments in f-string in Python

We cannot use comments inside F-string expressions. It will give an error:

Python
f"GeeksforGeeks is {5*2 + 3 #geeks-5} characters."

Output:

Hangup (SIGHUP)
  File "Solution.py", line 1
    f"GeeksforGeeks is {5*2 + 3 #geeks-5} characters."
    ^
SyntaxError: f-string expression part cannot include '#'

Printing Braces using f-string in Python

  • To display curly braces in an f-string output, use double curly braces {{ }}.
  • Each pair of braces in the output requires double braces in the f-string.
  • Example: print(f"{{Hello}}"), Output: {Hello}.
Python
# Printing single braces
print(f"{{Hello, Geek}}")

# Printing double braces
print(f"{{{{Hello, Geek}}}}")

Output
{Hello, Geek}
{{Hello, Geek}}

Article Tags :