I am learning the Python string format() method. Though I understand that {} is a placeholder for arguments, I am not sure what : represent in the following code snippet from Programiz tutorial:
import datetime # datetime formatting date = datetime.datetime.now() print("It's now: {:%Y/%m/%d %H:%M:%S}".format(date)) # custom __format__() method class Person: def __format__(self, format): if(format == 'age'): return '23' return 'None' print("Adam's age is: {:age}".format(Person())) - Why is there a
:in front of%Yinprint("It's now: {:%Y/%m/%d...? The code outputsIt's now: 2021, and there is no:in front of 2021. - Why is there a
:in front ofageinprint("Adam's age is: {:age}...?
Thanks in advance for your valuable input!!
:in front of an argument calls the__format__method of the object that you pass in to format.