Python - f-strings



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.

Example

In the below example, we are printing the variables using f-strings.

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 can also be used to evaluate expressions inside the {} placeholder.

Example

The following example shows how to evaluate expression using f-strings.

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. Dictionary is one of the built-in data types in Python that stores unordered collection of key-value pairs.

Example

In the following example, we are formatting a dictionary using f-strings.

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 as shown in the following example −

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. To do so, simply use the function name by passing required parameters.

Example

The below example shows how to call a method inside f-string.

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 when used with "x", "o" and "e" respectively.

Example

The below example shows how to format numbers using f-string.

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