Pedi Alz
Pedi Alz

Reputation: 21

What does this syntax mean in string formatting in Python?

I saw the syntax below in a tutorial

print("{0:>2} in binary is {0:>08b}".format(100))

I was wondering what does the greater equal sign (>) does?

and is it necessary to use it?

because when I used it like this

print("{0:2} in binary is {0:08b}".format(100))

it produced the same result as the first one I mentioned above

Upvotes: 1

Views: 50

Answers (1)

khelwood
khelwood

Reputation: 59104

> means right-aligned as opposed to <, left-aligned. Right-aligned is the default for numbers anyway. See https://docs.python.org/3/library/string.html#formatspec:

>

Forces the field to be right-aligned within the available space (this is the default for numbers).

Upvotes: 2

Related Questions