How are booleans formatted in Strings in Python?

后端 未结 4 1315
执念已碎
执念已碎 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:38

    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

提交回复
热议问题