Python string format() method
Insert values into the {} placeholders of a string.
Syntax
str.format(*args, **kwargs)
The older way to build strings from values. f-strings are shorter and usually preferred, but format is still useful for templates stored before the values are known.
Parameters
| *args, **kwargs | values for the placeholders |
Return value
str
Example
Code
print("{} scored {}".format("Sita", 91)) print("{name} is {age}".format(name="Leo", age=31)) template = "Dear {}, your total is ${:.2f}" print(template.format("Mia", 23.5))
Output
Sita scored 91 Leo is 31 Dear Mia, your total is $23.50
Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.