I searched for creating aligned strings in Python and found some relevant stuff, but didn't work for me. Here's one example:
for line in [[1, 128, 1298039], [123388, 0, 2]]:print('{:>8} {:>8} {:>8}'.format(*line))
Output:
1 128 1298039123388 0 2
This is what I see in the shell:
As you can see, the alignment didn't happen. Same problem arises when using \t
.
What can I do to align the strings in a neat, tabular format?
You have configured your IDLE shell to use a proportional font, one that uses different widths for different characters. Notice how the ()
pair takes almost the same amount of horizontal space as the >
character above it.
Your code is otherwise entirely correct; with a fixed-width font the numbers will line up correctly.
Switch to using a fixed width font instead. Courier is a good default choice, but Windows has various other fonts installed that are proportional, including Consolas.
Configure the font in the Options -> Configure IDLE menu. Pick a different font from the Font Face list. The sample characters in the panel below should line up (except for the second line k
at the end, it should stick out).