How to use f-string In Python

This method was introduced in Python 3.6 version. This is much easier than using a format specifier or a format() method. The f-strings are expressions that are evaluated in the runtime and are formatted by the __format__() method. For formatting a string using the f-string, the formatting expression must be written in quotes preceded by ‘f’/’F’.

Syntax : f “<text> {placeholder} <text>” or F”<text> {placeholder} <text>”

Example 1

The name and age are variables. They can be formatted and printed with the help of f-string by placing the variable in the appropriate placeholders. The placeholders are just the curly braces found within the texts.

Python3




name = "Rinku"
age = 20
  
print(f"Hi! my name is {name} and my age is {age}")


Output

Hi! my name is Rinku and my age is 20

Example 2:

Expressions can also be evaluated and displayed using this f-string method.

Python3




a = 5
b = 6
  
print(F"Addition : {a+b}")
print(F"Subtraction : {a-b}")
print(F"Multiplication : {a*b}")
print(F"Division without roundoff : {a/b}")
print(F"Division with roundoff : {round(a/b,3)}")


Output

Addition : 11
Subtraction : -1
Multiplication : 30
Division without roundoff : 0.8333333333333334
Division with roundoff : 0.833

Example 3:

Even functions can be called from the f-string placeholders. A function to evaluate the expression a2 + 3b2 + ab is defined and called within the placeholder.

Python3




# f function to evaluate expression
def exp_eval(a, b):
    ans = (a*a)+(3*(b*b))+(a*b)
    return ans
  
  
# values to be evaluated
a = 2
b = 4
  
# formatting
print(f"The expression a**2+3b**2+(a*b) = {exp_eval(a,b)}")


Output

The expression a**2+3b**2+(a*b) = 60


How to use string formatters in Python ?

The output displayed as a result of a program should be organized in order to be readable and understandable. The output strings can be printed and formatted using various ways as listed below.

  1. Using print()
  2. Using format specifiers.
  3. Using format() method.
  4. Using formatted string literal (f-string).

This article covers the first three methods given above.

Similar Reads

Using print()

The print() function is used to display any message or output of processes carried out in Python. This function can be used to print strings or any object. But, before printing the object that is not of string type, print() converts it to a string. The string can be formatted either with the parameters of this method itself or using the rest of the methods covered in this article. The separator parameter (sep) of this function can help to display the desired symbol in between each element being printed....

Using format specifiers

...

Using format() method

The format specifiers are the ones that we use in languages like C-programming. Python does not have a printf() as in C, but the same functionality is provided here. The modulo operator (‘%’) is overloaded by the string class for formatting the output. The % operator formats the set of variables contained within a tuple. If multiple format specifiers are chained, then the first specifier acts on the 0th element in the tuple, the second acts on 1st  element in the tuple and so on. The format specifiers are given in the table below....

Using f-string

...