I was trying to print a truth table for Boolean expressions. While doing this, I stumbled upon the following:
>>> format(True, "") # shows True in a string representation, same as str(True)
'True'
>>> format(True, "^") # centers True in the middle of the output string
'1'
As soon as I specify a format specifier, format()
converts True
to 1
. I know that bool
is a subclass of int
, so that True
evaluates to 1
:
>>> format(True, "d") # shows True in a decimal format
'1'
But why does using the format specifier change 'True'
to 1
in the first example?
I turned to the docs for clarification. The only thing it says is:
A general convention is that an empty format string (
""
) produces the same result as if you had calledstr()
on the value. A non-empty format string typically modifies the result.
So the string gets modified when you use a format specifier. But why the change from True
to 1
if only an alignment operator (e.g. ^
) is specified?
format(str(True),"^")
– Youlandayoulton'^'
operator, although that is weird. Also note that'^'
is the align-center operator, not the width operator. – Decahedron