Jul-25-2019, 12:28 PM
It's formatted one time. Here the proof of it:
# explaining with code class MyFormat(str): def __format__(self, fmt_str): print('Magic method __format__ was called.') fmt_str = fmt_str.strip() if 'UPPER' == fmt_str: return self.upper() elif 'LOWER' == fmt_str: return self.lower() else: return self my_str = MyFormat('Hello World') # 3 calls, 3 times formatting print(f'{my_str: UPPER }') print(f'{my_str: LOWER }') print(f'{my_str}') # the same with the format method # 3 calls, 3 times formatting print('{:UPPER}'.format(my_str)) print('{:LOWER }'.format(my_str)) print('{}'.format(my_str))
Output:Magic method __format__ was called.
HELLO WORLD
Magic method __format__ was called.
hello world
Magic method __format__ was called.
Hello World
Magic method __format__ was called.
HELLO WORLD
Magic method __format__ was called.
hello world
Magic method __format__ was called.
Hello World
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!