繁体   English   中英

Python格式化的String

[英]Python formatted String

计划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

如何使用Formatted String Literals在右侧缩进

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM