How are booleans formatted in Strings in Python?


Question

I see I can't do:

"%b %b" % (True, False)

in Python. I guessed %b for b(oolean). Is there something like this?

1
150
6/8/2014 1:16:51 AM

Accepted Answer

>>> print "%r, %r" % (True, False)
True, False

This is not specific to boolean values - %r calls the __repr__ method on the argument. %s (for str) should also work.

222
2/13/2010 10:07:31 PM

If you want True False use:

"%s %s" % (True, False)

because str(True) is 'True' and str(False) is 'False'.

or if you want 1 0 use:

"%i %i" % (True, False)

because int(True) is 1 and int(False) is 0.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon