1

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

Hash
  • 455
  • 3
  • 14
  • 1
    https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces something like this? Specifically https://stackoverflow.com/a/38228621/3134251 this answer. – Lafexlos Jul 19 '19 at 13:38
  • 1
    the simple `'{:10}9'` doesn't work for you because you're formatting a number. It will work if it was a string. You can either do `print('{:10}9'.format(str(12345)))` or as suggested in the answer – Tomerikoo Jul 19 '19 at 13:41

1 Answers1

3

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

RomanPerekhrest
  • 73,078
  • 4
  • 37
  • 76