I have to output a collection of strings and integer values such that there is one string followed by an integer in one line. the problem i am having is to arrange it properly on the display

Example:
I am trying to print: or rather what i am able to print is

zmike russel  23
stephanie davidson 45
timmy 19

Now I trying to make it display:

mike russel             23
stephanie davidson      45
timmy                   19

i.e the names should be in one column and the digits in one column. I am finding it hard to realise this using the print statement. The numbers just don't come exactly below one another ......

Please help ....:?:

Recommended Answers

All 4 Replies

Like this?

print "%-20s %4d" % ("John Smith",107)
print "%-20s %4d" % ("Phineas T. Fogg", 80)

John Smith 107
Phineas T. Fogg 80

Familiarize yourself with the % operator, with operands of the form <string> % <sequence> (as opposed to numeric operands, where % is quite different).

Thanks ... that worked ...

though am not very sure how ?? ..... google didn't help me much from the lead you had given

how wexactly the offsets besides the % signs work ??

Thanks ... that worked ...

though am not very sure how ?? ..... google didn't help me much from the lead you had given

how wexactly the offsets besides the % signs work ??

print "%-[B]20[/B]s %4d" % ("John Smith",107) % Is the conversion specifier. It tells print that what it follows is the formula you want for displaying the data. As part of that formula you have ( - ) which means you want the data to be align to the left in a minimum field of 20, that's what that 20 is. If the string is less that 20 spaces will be use to add up to 20.

Now the %4d is another format formula to display an integer aligned to the right in a minimum field of 4, displaying 107 as space,1,0,7 right after where the first formated string left it.

The % without color indicates to print, "Here there are, the actual objects to which I want you to apply these formulas to".

The full writeup on the % operator (as well as the % format specifier :-) is here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.