Python uses f-string to format strings

Python Formatting Strings with f-strings

Starting with version 3.6, Python introduced a new string formatting method, f-strings, or literal string interpolation. Using this formatting method, you can embed Python expressions within string constants. Python f-strings are faster, more readable, more concise, and less error-prone.

A string begins with the ‘f’ prefix and inserts one or more placeholders whose values are dynamically filled.

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

This will produce the following output.

My name is Rajesh and I am 23 years old

f-strings can contain expressions within the {} placeholders.

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

The following will produce the output −

Price: 10 Quantity: 3 Total: 30

Placeholders can be filled with dictionary values.

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

This will produce the following output −

My name is Ramesh and I am 23 years old

The equal sign (=) character is used for self-debugging f-string expressions.

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

This will produce the following output −

Total : price*quantity=30

User-defined functions can also be called from within f-string expressions.

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

price = 10
quantity = 3

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

This will produce the following output –

Price: 10 Quantity: 3 Total: 30

Python’s f-strings also support formatting floating-point numbers using precision specifications, much like the format() method and the % 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)

This will produce the following output −

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

For string variables, you can specify alignment just as with the format() method and the % 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)

This 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

f-strings can display numbers in 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)

Here is the output it will produce –

Hexadecimal : 14
Octal :24
Scientific notation: 2.000000e+01

Leave a Reply

Your email address will not be published. Required fields are marked *