How are booleans formatted in Strings in Python?

PythonBooleanString Formatting

Python Problem Overview


I see I can't do:

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

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

Python Solutions


Solution 1 - Python

>>> 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.

Solution 2 - Python

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.

Solution 3 - Python

You may also use the Formatter class of string

print "{0} {1}".format(True, False);
print "{0:} {1:}".format(True, False);
print "{0:d} {1:d}".format(True, False);
print "{0:f} {1:f}".format(True, False);
print "{0:e} {1:e}".format(True, False);

These are the results

True False
True False
1 0
1.000000 0.000000
1.000000e+00 0.000000e+00

Some of the %-format type specifiers (%r, %i) are not available. For details see the Format Specification Mini-Language

Solution 4 - Python

To update this for Python-3 you can do this

"{} {}".format(True, False)

However if you want to actually format the string (e.g. add white space), you encounter Python casting the boolean into the underlying C value (i.e. an int), e.g.

>>> "{:<8} {}".format(True, False)
'1        False'

To get around this you can cast True as a string, e.g.

>>> "{:<8} {}".format(str(True), False)
'True     False'

Solution 5 - Python

Referring to Python string interpolation

In Python 3.6, string interpolation has been added with a new string literal f prefix

shouldBeTrue = True
print(f"shouldBeTrue={shouldBeTrue}")

Solution 6 - Python

To expand on the answer by phd, you can do all that without str()-ing your booleans in the format() call. There are some weird tricks you can exploit to do formatting for booleans with the new style of string interpolation. It also works for f-strings.

The bland, trivial case
'{},{}'.format(True, False)  # 'True,False'
f'{True},{False}'            # 'True,False'
Formatting converts booleans to integers

Any padding length will work.

'{:0},{:0}'.format(True, False)  # '1,0'
f'{True:0},{False:0}'            # '1,0'

'{:>5},{:>5}'.format(True, False)  # '    1,    0'
f'{True:>5},{False:>5}'            # '    1,    0'
String coercion can bring back the normal text

Note the !s in there. This is the most direct equivalent to %s. There's also !r for __repr__() and !a for ASCII, but they're not particularly interesting for booleans.

'{!s:>5},{!s:>5}'.format(True, False)  # ' True,False'
f'{True!s:>5},{False!s:>5}'            # ' True,False'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuanjo ContiView Question on Stackoverflow
Solution 1 - PythondanbenView Answer on Stackoverflow
Solution 2 - PythonDesintegrView Answer on Stackoverflow
Solution 3 - PythonWolfView Answer on Stackoverflow
Solution 4 - Pythonirritable_phd_syndromeView Answer on Stackoverflow
Solution 5 - PythonChinLoongView Answer on Stackoverflow
Solution 6 - PythonMichaelView Answer on Stackoverflow