简体   繁体   中英

Python formatted String

program 1:

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

output of program 1:

     123459
   89739559

program 2:

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

output of program 2:

     123459
   89739559

There is only one difference between the two programs. In the first program I used -10 for extra indentation. In the 2nd program I used 10 for extra indentation. Both -10 and 10 are giving indentation on the left side. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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