0

Instead of 007, I wish to print 117, using string formatting.

But instead, it adds 12 empty characters before the 7. Tried using a variable too, in place of the 1. No luck. Please help.

Code:

num=7

print(f'{num:03d}')

print(f'{num:13d}')


val=1

print(f'{num:{val}3d}')

Code

Output

Klaus D.
  • 13,874
  • 5
  • 41
  • 48

1 Answers1

3

This works:

num=7
print(f'{num:03d}')
print(f'{num:13d}')
print(f'{num:1>3d}')
val=1
print(f'{num:{val}>3d}')

output

007
            7
117
117

To avoid ambiguity, always specify the alignment option.

buran
  • 13,682
  • 10
  • 36
  • 61