Home >>Python String Methods >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 | Description |
---|---|
value1, value2... | This is a required parameter. It defines one or more values that should be formatted and inserted in the string. |
txt = "Price of this product is only {price:.2f} dollars!"
print(txt.format(price = 68))
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)