Home >>Python String Methods >Python String format() Method

Python String format() Method

Python String format() Method

Python string format() method is used to format the required value and insert them inside the string's placeholder. This placeholder is defined using curly brackets: {}. It returns the formatted string.

Syntax:
string.format(value1, value2...)

Parameter Values

Parameter Description
value1, value2... This is a required parameter. It defines one or more values that should be formatted and inserted in the string.
Here is an example of Python format() method:

txt = "Price of this product is only {price:.2f} dollars!"
print(txt.format(price = 68))

Output:
Price of this product is only 68.00 dollars!
Example 2:

txt1 = "My name is {fname}, I'm {age}".format(fname = "Jerry", age = 21)
txt2 = "My name is {0}, I'm {1}".format("Jerry",21)
txt3 = "My name is {}, I'm {}".format("Jerry",21)
print(txt1)
print(txt2)
print(txt3)

Output:
My name is Jerry, I'm 21
My name is Jerry, I'm 21
My name is Jerry, I'm 21

No Sidebar ads