在python中使用模板格式

在python中使用模板格式,python,templates,Python,Templates,我使用此代码来对齐输出 template = "{0:20}{1:5}" print template.format("1","bread") 1 bread 2 cheese 但是如果我希望输出是这样的呢 1 .... bread 2 .... cheese 有人能帮我吗?根据,您需要在code20/code之前添加>>template=“{0:。也许您应该试试 template = "{0:2}.... {1:5}" print template.format("1

我使用此代码来对齐输出

template = "{0:20}{1:5}"
print template.format("1","bread")

1      bread
2      cheese
但是如果我希望输出是这样的呢

1 .... bread
2 .... cheese
有人能帮我吗?

根据,您需要在code20/code之前添加
>>template=“{0:。也许您应该试试

template = "{0:2}.... {1:5}"
print template.format("1", "bread")

1 .... bread
如果你想要一个可变数量的点,你可以试试

template = "{0:.<20}{1:5}"
print template.format("1", "bread")

1....................bread

template=“{0:。您考虑过在模板字符串中添加点吗?
template = "{0:2}.... {1:5}"
print template.format("1", "bread")

1 .... bread
template = "{0:.<20}{1:5}"
print template.format("1", "bread")

1....................bread