【问题标题】:Python formatted StringPython 格式化字符串
【发布时间】:2019-11-28 12:28:55
【问题描述】:

程序 1:

print('{:-10}9'.format(12345))
print('{:-10}9'.format(8973955))

程序1的输出:

     123459
   89739559

程序 2:

print('{:10}9'.format(12345))
print('{:10}9'.format(8973955))

程序2的输出:

     123459
   89739559

这两个程序只有一个区别。在第一个程序中,我使用 -10 进行额外缩进。在第二个程序中,我使用 10 进行额外缩进。 -10 和 10 都在左侧缩进。但我想在右侧做缩进,这样我就可以产生这样的输出:

12345     9
8973955   9

如何使用格式化字符串文字在右侧缩进

【问题讨论】:

标签: python python-3.x string io python-textprocessing


【解决方案1】:

指定对齐的方向(对齐选项):

print('{:<10}9'.format(12345))
print('{:<10}9'.format(8973955))

输出:

12345     9
8973955   9

https://docs.python.org/3.4/library/string.html#format-specification-mini-language

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2016-06-21
    相关资源
    最近更新 更多