format() method in Python
The format() method is used to format strings by replacing placeholders in the string with specified values. The placeholder is defined using curly brackets {}.
Syntax
string.format(value 1,value 2 ..., valueN)
Parameters
- value 1,value 2 - One or more values that should be formatted and inserted into the string.Any data type can be included in the values.
The following example shows, how works the format() method.
Example
#named indexes: a = "My name is {name}, I'm studying {stud} th standard".format(name = "Sam", stud= 12) #numbered indexes: b = "My name is {0}, Coming From {1}".format("Ram","Canada") #empty placeholders: c = "My name is {}, I'm {}".format("Saraa",15) print(a) print(b) print(c)
Output
My name is Sam, I'm studying 12 th standard My name is Ram, Coming From Canada My name is Saraa, I'm 15