“”” there are three methods to use format()
- use nothing inside placeholder (curly brackets, but we can customize) and give the variables inside format()””” price = 49 txt = “The price is {} dollars” txt.format(price)
#let’s customize the placeholder txt = “The price is {:.2f} dollars” #2f means two floating points after the value txt = “The price is {:.2f} dollars” txt.format(price)
“”“2. Add index number of the parameters (variable names) inside placeholder and give variable names inside format()””””
quantity = 3 itemno = 567 price = 49
myorder = “I want {0} pieces of item number {1} for {2:.2f} dollars.”
print(myorder.format(quantity, itemno, price))
“”” 3. Set name to the parameters inside placeholder and give values of the parameters inside format() “”” myorder = “I have a {carname}, it is a {model}.” print(myorder.format(carname = “Ford”, model = “Mustang”))
https://www.w3schools.com/python/python_string_formatting.asp