Python print

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 7
Reputation: Metahuman is an unknown quantity at this point 
Solved Threads: 0
Metahuman Metahuman is offline Offline
Newbie Poster

Python print

 
0
  #1
Mar 31st, 2008
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


  1. zmike russel 23
  2. stephanie davidson 45
  3. timmy 19

Now I trying to make it display:

  1. mike russel 23
  2. stephanie davidson 45
  3. 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 ....
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Python print

 
0
  #2
Mar 31st, 2008
Like this?
  1. print "%-20s %4d" % ("John Smith",107)
  2. print "%-20s %4d" % ("Phineas T. Fogg", 80)
  1. John Smith 107
  2. 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).
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 7
Reputation: Metahuman is an unknown quantity at this point 
Solved Threads: 0
Metahuman Metahuman is offline Offline
Newbie Poster

Re: Python print

 
0
  #3
Mar 31st, 2008
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 ??
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,044
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Python print

 
0
  #4
Mar 31st, 2008
Originally Posted by Metahuman View Post
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 "%-20s %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".
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Python print

 
0
  #5
Apr 1st, 2008
The full writeup on the % operator (as well as the % format specifier :-) is here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC