202

I see I can't do:

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

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

3
  • 1
    What do you want the result to be? True and False? You want '%s' then. %b, when it exists, is for binary (as in base-2). Commented Feb 13, 2010 at 22:05
  • 2
    Ho yes! "%s" seems to work just fine. Reply this question and get a lected answer. I used to do "%s" % str(False). Shame on me :) Commented Feb 13, 2010 at 22:07
  • 3
    link to relevant documentation: docs.python.org/library/… Commented Feb 13, 2010 at 23:07

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

What's the major difference between %r and %s?
I always had this distiction in mind, but correct me if I'm wrong. %s (and thus str()) aim to represent the object as transparantly as possible for humans. %r (and thus repr()) aim to represent the object as transparantly as possible for python. For example, print(str("foo")) merely prints foo on a new line. print(repr("foo")) however prints 'foo' on a new line, including the quotes, since that's what you need to type in the python interpreter to get the corresponding object to the argument in python.
82

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.

Comments

21

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

2 Comments

Note that adding formatting options can screw this up. E.g. {:_^5} will get you '__1__' unless you str(...) the booleans.
You can get around the problems with type conversions like !s: '{!s:_^5}'.format(True) is 'True_'
8

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'

Comments

4

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'

Adding some formatting converts booleans to integers

Any padding length will do this. I am not sure why someone would want it to work this way.

'{: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'

Comments

0

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}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.