Python 中的字符串中的布尔值是如何格式化的?

发布于 2024-08-20 23:40:42 字数 119 浏览 5 评论 0原文

我发现我做不到:

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

用Python。我猜 %b 为 b(olean)。有这样的事吗?

I see I can't do:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静赏你的温柔 2024-08-27 23:40:42
>>> print "%r, %r" % (True, False)
True, False

这并非特定于布尔值 - %r 对参数调用 __repr__ 方法。 %s(对于str)也应该有效。

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

墨落成白 2024-08-27 23:40:42

如果您想要 True False 使用:

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

因为 str(True)'True' 并且 str(False)“假”

或者如果您想要 1 0 使用:

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

因为 int(True)1 并且 int(False) 是 <代码>0。

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.

冰火雁神 2024-08-27 23:40:42

您还可以使用 string 的 Formatter 类

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);

这些是结果

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

某些 % 格式类型说明符(%r%i)不可用。有关详细信息,请参阅迷你语言格式规范

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

冬天旳寂寞 2024-08-27 23:40:42

要更新Python-3,你可以这样做

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

但是,如果你想实际格式化字符串(例如添加空格),你会遇到Python将布尔值转换为基础C值(即int),例如

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

要解决这个问题,你可以将 True 转换为字符串,例如

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

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'
ま柒月 2024-08-27 23:40:42

要扩展 phd 的答案,您可以在不使用 str()-ing 的情况下完成所有这些操作format() 调用中的布尔值。您可以利用一些奇怪的技巧来使用新的字符串插值样式对布尔值进行格式化。它也适用于 f 弦。

平淡、琐碎的情况

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

添加一些格式将布尔值转换为整数

任何填充长度都可以做到这一点。我不确定为什么有人希望它以这种方式工作。

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

字符串强制转换可以恢复正常文本

注意!s 在那里。这是与 %s 最直接的等同。还有用于 __repr__()!r 和用于 ASCII 的 !a,但它们对于布尔值并不是特别有趣。

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

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'
深府石板幽径 2024-08-27 23:40:42

参考Python字符串插值

在Python 3.6中,字符串插值已经添加了新字符串文字 f 前缀

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

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}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文