Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

aligning print statements in python

Name: Anonymous 2011-09-13 17:53

I'm trying to format my output so that everything lines up properly along the left side. I'm also rounding everything to 2 decimal places. The decimals are fine, however there isn't a proper number of spaces between Length and then the value.


print('Length: %10.2f' % length)
print('Width: %10.2f' % width)

What I want:
Length:      20.22
Width:       40.23

I realize this may seem like a very elementary question, but I've given myself a headache over the last hour trying to do this myself.

Name: Anonymous 2011-09-13 17:56

Use tabs or .format

Name: Anonymous 2011-09-13 17:58

\t

Name: Anonymous 2011-09-13 18:10

>>3

I can't use \t because I need to ensure that even if I got a number like 302, it would still be lined properly. If anyone can go into greater detail on using the .format function I'd appreciate it.

another example:
Length:    20.22
Width:     40.23
Density:  302.33

Name: Anonymous 2011-09-13 18:26

Forward pad all strings that are shorter than the longest one until they are all the same length. This question might be better suited to /g/, I suggest you go back there.

Don't change these.
Name: Email:
Entire Thread Thread List