此语法在 Python 中的字符串格式中意味着什么?
What does this syntax mean in string formatting in Python?
我在教程中看到了下面的语法
print("{0:>2} in binary is {0:>08b}".format(100))
我想知道大等号 (>) 的作用是什么?
有必要使用吗?
因为我是这样用的
print("{0:2} in binary is {0:08b}".format(100))
它产生了与我上面提到的第一个相同的结果
>
表示右对齐,而不是 <
,左对齐。无论如何,右对齐是数字的默认设置。见 https://docs.python.org/3/library/string.html#formatspec:
>
Forces the field to be right-aligned within the available space (this is the default for numbers).
我在教程中看到了下面的语法
print("{0:>2} in binary is {0:>08b}".format(100))
我想知道大等号 (>) 的作用是什么?
有必要使用吗?
因为我是这样用的
print("{0:2} in binary is {0:08b}".format(100))
它产生了与我上面提到的第一个相同的结果
>
表示右对齐,而不是 <
,左对齐。无论如何,右对齐是数字的默认设置。见 https://docs.python.org/3/library/string.html#formatspec:
>
Forces the field to be right-aligned within the available space (this is the default for numbers).