Format Method

Format method

sentence = "The sum of 5 + 10 is {0}".format(50)
  • This will replace {0} with the value provided in the format method

  • You can pass multiple args as well:

sentence = "The sum of {0} + {0} is {0}".format(50)
# Since all the data is {0} it means that it will take only the first arg from the format function

sentence = "The sum of {0} + {1} is {2}".format(50, 10, 60)
# Here each arg will be taken accordingly and it will do:
# 50 + 10 = 60

Last updated