How are booleans formatted in Strings in Python?

后端 未结 4 1430
执念已碎
执念已碎 2021-01-30 06:13

I see I can\'t do:

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

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

4条回答
  •  渐次进展
    2021-01-30 06:30

    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'
    

提交回复
热议问题