I want to create a formatted string with fixed size with fixed position between fields. An example explains better, here there are clearly 3 distinct fields and the string is a fixed size:
XXX 123 98.00
YYYYY 3 1.00
ZZ 42 123.34
How can I apply such formatting to a string in python (2.7)?
Best Solution
Sure, use the .format method. E.g.,
will print
You can adjust the field sizes as desired. Note that
.format
works independently ofprint
to format a string. I just used print to display the strings. Brief explanation:There are many additional options to position/format strings (padding, left/right justify etc), String Formatting Operations will provide more information.
Update for f-string mode. E.g.,
For right alignment