如何在 Python 中格式化固定宽度的文本块?
How to format a block of text with fixed width in Python?
我正在尝试生成如下所示的字符串:
Nadya's Cell: (415) 123-4567
Jim's Cell: (617) 123-4567
其中名称和 phone 数字不同,需要插入,并且 phone 数字应该对齐。对于此示例,我使用了以下模板:
name1 = "Nadya"
name2 = "Jim"
phone_number1 = "(415) 123-4567"
phone_number2 = "(617) 123-4567"
string = "{name1}'s Cell: {phone_number1}\n{name2}'s Cell: {phone_number2}".format(**locals())
我希望字符串的总宽度适应 name1
和 name2
中最长的一个,而不是手动添加空格。
到目前为止,我所能想到的 https://docs.python.org/3.6/library/string.html 如下:
max_length = max(len(name1), len(name2))
# This should use max_length and contain "'s Cell:"
string = "{name1:<7}{phone_number1}\n{name2:<7}{phone_number2}".format(**locals())
print(string)
产生
Nadya (415) 123-4567
Jim (617) 123-4567
问题是总宽度 7
仍然硬编码到模板中,我不知道如何在名称后添加 's Cell:
,因为这会产生空格在名称和撇号之间。有什么解决办法吗?
您可以使用嵌套格式说明符,这是一个经常被忽视的功能:
max_length = max(len(name1), len(name2)) + 2
string = """{name1:<{max_length}}{phone_number1}
{name2:<{max_length}}{phone_number2}""".format(**locals())
print(string)
# output:
# Nadya (415) 123-4567
# Jim (617) 123-4567
documentation只是顺便提到了它们,这可能解释了为什么它们相对不为人知:
A format_spec field can also include nested replacement fields within
it. These nested replacement fields may contain a field name,
conversion flag and format specification, but deeper nesting is not
allowed. The replacement fields within the format_spec are substituted
before the format_spec string is interpreted. This allows the
formatting of a value to be dynamically specified.
这是另一个使用 f-strings 的解决方案,遵循 https://www.python.org/dev/peps/pep-0498/#format-specifiers:
name1 += "'s Cell:"
name2 += "'s Cell:"
max_length = max(len(name1), len(name2))
string = f"{name1:{max_length+2}}{phone_number1}\n{name2:{max_length+2}}{phone_number2}"
print(string)
产生
Nadya's Cell: (415) 123-4567
Jim's Cell: (617) 123-4567
根据需要。
我正在尝试生成如下所示的字符串:
Nadya's Cell: (415) 123-4567
Jim's Cell: (617) 123-4567
其中名称和 phone 数字不同,需要插入,并且 phone 数字应该对齐。对于此示例,我使用了以下模板:
name1 = "Nadya"
name2 = "Jim"
phone_number1 = "(415) 123-4567"
phone_number2 = "(617) 123-4567"
string = "{name1}'s Cell: {phone_number1}\n{name2}'s Cell: {phone_number2}".format(**locals())
我希望字符串的总宽度适应 name1
和 name2
中最长的一个,而不是手动添加空格。
到目前为止,我所能想到的 https://docs.python.org/3.6/library/string.html 如下:
max_length = max(len(name1), len(name2))
# This should use max_length and contain "'s Cell:"
string = "{name1:<7}{phone_number1}\n{name2:<7}{phone_number2}".format(**locals())
print(string)
产生
Nadya (415) 123-4567
Jim (617) 123-4567
问题是总宽度 7
仍然硬编码到模板中,我不知道如何在名称后添加 's Cell:
,因为这会产生空格在名称和撇号之间。有什么解决办法吗?
您可以使用嵌套格式说明符,这是一个经常被忽视的功能:
max_length = max(len(name1), len(name2)) + 2
string = """{name1:<{max_length}}{phone_number1}
{name2:<{max_length}}{phone_number2}""".format(**locals())
print(string)
# output:
# Nadya (415) 123-4567
# Jim (617) 123-4567
documentation只是顺便提到了它们,这可能解释了为什么它们相对不为人知:
A format_spec field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the format_spec string is interpreted. This allows the formatting of a value to be dynamically specified.
这是另一个使用 f-strings 的解决方案,遵循 https://www.python.org/dev/peps/pep-0498/#format-specifiers:
name1 += "'s Cell:"
name2 += "'s Cell:"
max_length = max(len(name1), len(name2))
string = f"{name1:{max_length+2}}{phone_number1}\n{name2:{max_length+2}}{phone_number2}"
print(string)
产生
Nadya's Cell: (415) 123-4567
Jim's Cell: (617) 123-4567
根据需要。