简体   繁体   English

Python格式化的String

[英]Python formatted String

program 1: 计划1:

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

output of program 1: 程序1的输出:

     123459
   89739559

program 2: 计划2:

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

output of program 2: 计划2的输出:

     123459
   89739559

There is only one difference between the two programs. 两个程序之间只有一个区别。 In the first program I used -10 for extra indentation. 在第一个程序中,我使用-10进行额外缩进。 In the 2nd program I used 10 for extra indentation. 在第二个程序中,我使用了10个额外的缩进。 Both -10 and 10 are giving indentation on the left side. -10和10都在左侧给出压痕。 But I want to do indentation on the right side so that I can produce output like this: 但我想在右侧做缩进,以便我可以产生这样的输出:

12345     9
8973955   9

How can I indent in the right side using Formatted String Literals 如何使用Formatted String Literals在右侧缩进

Specify the direction of alignment (alignment option): 指定对齐方向 (对齐选项):

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

The output: 输出:

12345     9
8973955   9

https://docs.python.org/3.4/library/string.html#format-specification-mini-language 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