Format An Integer To Show Leading Spaces

Format An Integer To Show Leading Spaces

isocam
Collaborator Collaborator
277 Views
1 Reply
Message 1 of 2

Format An Integer To Show Leading Spaces

isocam
Collaborator
Collaborator

Can anybody help?

 

How do you format an integer to show leading spaces?

 

For example,

 

1 should be          "   1"                    (3-leading spaces)

10 should be       "  10"                   (2-leading spaces)

100 should be    " 100"                  (1-leading space)

1000 should be "1000"                  (No leading spaces)

 

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
278 Views
1 Reply
Reply (1)
Message 2 of 2

kandennti
Mentor
Mentor
Accepted solution

Hi @isocam .

 

Try this.

        numbers = [1,10,100,1000]
        for num in numbers:
            print('{:>4}'.format(num))
0 Likes