Format output string, right alignment
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle
--
Chapters
00:00 Format Output String, Right Alignment
00:41 Accepted Answer Score 328
01:02 Answer 2 Score 69
01:12 Answer 3 Score 81
01:46 Answer 4 Score 40
02:00 Thank you
--
Full question
https://stackoverflow.com/questions/8234...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #alignment #stringformatting
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle
--
Chapters
00:00 Format Output String, Right Alignment
00:41 Accepted Answer Score 328
01:02 Answer 2 Score 69
01:12 Answer 3 Score 81
01:46 Answer 4 Score 40
02:00 Thank you
--
Full question
https://stackoverflow.com/questions/8234...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #alignment #stringformatting
#avk47
ACCEPTED ANSWER
Score 328
Try this approach using the newer str.format syntax:
line_new = '{:>12} {:>12} {:>12}'.format(word[0], word[1], word[2])
And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):
line_new = '%12s %12s %12s' % (word[0], word[1], word[2])
ANSWER 2
Score 81
You can align it like that:
print('{:>8} {:>8} {:>8}'.format(*words))
where > means "align to right" and 8 is the width for specific value.
And here is a proof:
>>> for line in [[1, 128, 1298039], [123388, 0, 2]]:
print('{:>8} {:>8} {:>8}'.format(*line))
1 128 1298039
123388 0 2
Ps. *line means the line list will be unpacked, so .format(*line) works similarly to .format(line[0], line[1], line[2]) (assuming line is a list with only three elements).
ANSWER 3
Score 69
It can be achieved by using rjust:
line_new = word[0].rjust(10) + word[1].rjust(10) + word[2].rjust(10)
ANSWER 4
Score 40
I really enjoy a new literal string interpolation in Python 3.6+:
line_new = f'{word[0]:>12} {word[1]:>12} {word[2]:>12}'
Reference: PEP 498 -- Literal String Interpolation