Jason
Jason

Reputation: 17

Wrong string formatting?

Here's the program:

layout = "{0:>5}"
layout += "{1:>10}"
for i in range(2, 13):
    layout += "{"+str(i)+":9>}"


index = []
for i in range(13):
    index.append(i)
index = tuple(index)
print(layout.format(*index))

and it prints out like this:

    0         123456789101112

but I want it to look something like this(number of spaces might be wrong):

    0    1   2   3  4  5  6  7  8  9  10  11  12

What did I do wrong?

Upvotes: 0

Views: 71

Answers (1)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

":9>}"

should be

":>9}"

This gives:

    0         1        2        3        4        5        6        7        8        9       10       11       12

To look exactly like what you ask:

Actually, you're asking for something weird, but here's a more succint way to write what you wrote:

layout = "{0:>5}{1:>5}" + ''.join("{" + str(i) + ":>4}" for i in range(2, 13))
print(layout.format(*range(13)))

Gives:

    0    1   2   3   4   5   6   7   8   9  10  11  12

Upvotes: 2

Related Questions