Python – f strings

Hi all,

Today in this article we are going to discuss about f strings in Python

What are f strings?

f strings are formatted strings, if we do not use f string the names of variable is printed as it is. Python f-strings provide a faster, more readable, gives lot of information clearly in few words in , and less error prone way of formatting strings in Python.

We use f – strings when a variable contains lot of information ad we need to print that information with more text.

for example –

name = ("Sayyam Sabadra")
age = (12)
print("The age of {name} is {age} years")
The age of {name} is {age} years

But if we use f string it prints what is store in the variable for example –

name = ("Sayyam Sabadra")
age = (12)
print(f"The age of {name} is {age} years")
The age of Sayyam Sabadra is 12 years

Python Program to print today’s date using datetime module an f string

# Prints today's date with help
# of datetime library
import datetime
today =
print(f"{today:%B %d, %Y}")
April 10, 2022

How To Use str.format()

This is same as f string but we leave empty curly braces and in last need to type .format( , )

name = ("Sayyam Sabadra")
age = (12)
print("Hello {} you are {} years old".format(name, age))
Hello Sayyam Sabadra you are 12 years old

It would also be valid to use a capital letter F:

name = ("Sayyam Sabadra")
age = (12)
print(F"The age of {name} is {age} years")
The age of Sayyam Sabadra is 12 years

We can also have Multiline f strings

name = ("Sayyam")
age = (12)
programminglanguage = ("Python")

print(f"Hi {name} ")
print(f"You are a {age} years old")
print(f"You likes {programminglanguage}")
Hi Sayyam 
You are a 12 years old
You likes Python

Thank you

Have a nice day!

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: