Python - f-strings



String Interpolation and Formatting Using Python f-string

With the version 3.6, Python introduced a new string formatting method, f-strings or Literal String Interpolation. With this formatting method you can use embedded Python expressions inside string constants. Python f-strings are a faster, more readable, more concise, and less error prone.

Print Variables Using f-string

To print variables using f-strings, you can simply put them inside the placeholders ({}). The string starts with a 'f' prefix, and one or more place holders are inserted in it, whose value is filled dynamically.

name = 'Rajesh'
age = 23
fstring = f'My name is {name} and I am {age} years old'
print (fstring)

It will produce the following output

My name is Rajesh and I am 23 years old

Evaluate Expressions Using f-Strings

The f-string may contain expressions inside the {} placeholder.

price = 10
quantity = 3
fstring = f'Price: {price} Quantity : {quantity} Total : {price*quantity}'
print (fstring)

It will produce the following output

Price: 10 Quantity : 3 Total : 30

Printing Dictionaries Key-Value Using f-string

The placeholders can be populated by dictionary values.

user = {'name': 'Ramesh', 'age': 23}
fstring = f"My name is {user['name']} and I am {user['age']} years old"
print (fstring)

It will produce the following output

My name is Ramesh and I am 23 years old

Self Debugging Expression in f-string

The = character is used for self debugging the expression in f-string.

price = 10
quantity = 3
fstring = f"Total : {price*quantity=}"
print (fstring)

It will produce the following output

Total : price*quantity=30

Call User-Defined Function Using f-string

It is also possible to call a user defined function inside the f-string expression.

def total(price, quantity):
   return price*quantity

price = 10
quantity = 3

fstring = f'Price: {price} Quantity : {quantity} Total : {total(price, quantity)}'
print (fstring)

It will produce the following output

Price: 10 Quantity : 3 Total : 30

Precision Handling Using f-string

Python f-strings also support formatting floats with precision specifications, as done by format() method and string formatting operator %

name="Rajesh"
age=23
percent=55.50

fstring = f"My name is {name} and I am {age} years old and I have scored {percent:6.3f} percent marks"
print (fstring)

It will produce the following output

My name is Rajesh and I am 23 years old and I have scored 55.500 percent marks

String Alignment Using f-string

For string variable, you can specify the alignment just as we did with format() method and formatting operator %.

name='TutorialsPoint'
fstring = f'Welcome To {name:>20} The largest Tutorials Library'
print (fstring)

fstring = f'Welcome To {name:<20} The largest Tutorials Library'
print (fstring)

fstring = f'Welcome To {name:^20} The largest Tutorials Library'
print (fstring)

It will produce the following output

Welcome To       TutorialsPoint The largest Tutorials Library
Welcome To TutorialsPoint       The largest Tutorials Library
Welcome To    TutorialsPoint    The largest Tutorials Library

Print Numbers in Other Formats Using f-string

The f-strings can display numbers with hexadecimal, octal and scientific notation

num= 20
fstring = f'Hexadecimal : {num:x}'
print (fstring)

fstring = f'Octal :{num:o}'
print (fstring)

fstring = f'Scientific notation : {num:e}'
print (fstring)

It will produce the following output

Hexadecimal : 14
Octal :24
Scientific notation : 2.000000e+01
python_string_formatting.htm
Advertisements