| | |
Python print
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Solved Threads: 0
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
Now I trying to make it display:
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 ....
Example:
I am trying to print: or rather what i am able to print is
Python Syntax (Toggle Plain Text)
zmike russel 23 stephanie davidson 45 timmy 19
Now I trying to make it display:
Python Syntax (Toggle Plain Text)
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 ....
Like this?
Familiarize yourself with the % operator, with operands of the form
Python Syntax (Toggle Plain Text)
print "%-20s %4d" % ("John Smith",107) print "%-20s %4d" % ("Phineas T. Fogg", 80)
Python Syntax (Toggle Plain Text)
John Smith 107 Phineas T. Fogg 80
<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 ??
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
![]() |
Similar Threads
- Starting Python (Python)
- Generating HTML in Python (Python)
- "Hosts script: Python" (Python)
- Is there any place where you can find a complete list of python commands? (Python)
- very 1st python tutorial 4 newbies (Python)
Other Threads in the Python Forum
- Previous Thread: eliminating some random particle in an animation
- Next Thread: pycage
| Thread Tools | Search this Thread |
Tag cloud for Python
apache application array beginner book builtin calculator chmod code color converter countpasswordentry curved dan08 dictionary dynamic edit examples excel file filename float format ftp function gui homework import inches input java library line lines linux list lists loop mouse movingimageswithpygame mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime program programming projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software ssh stamp statictext string strings syntax table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






