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

1
  • 1
    .rjust(3, '1') Commented Jul 7, 2021 at 7:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.