[Tutor] Adding line numbers to a Python Script
Sudip Bhattacharya
sudipb at sudipb.com
Sat Jun 12 12:34:44 CEST 2010
I quote this example (and the problem that I am facing trying to understand)
from the book "Beginning Python - From Novice to Professional" by Magnus Lie
Hetland
Page 227
# numberlines.py
import fileinput........................(1)
for line in fileinput.input(inplace = true)............(2)
line = line.rstrip()........................................(3)
num=fileinput.lineno()..................................(4)
print '%-40s #%2i' %(line,num).....................(5)
Question: Please help me understand:
Line (3) - Are we stripping the entire line of code first, inserting the
code at the end (line (5)) and then placing it back at the same place ?
Line (5) - What does the individual characters in the expression "Print
'%-40s #%2i' %(line,num)" mean ?
- '-40s' : is this the space [what does "-40s" mean] that we are
creating post - which the line nos is inserted "#%2i" ?
- What does '%2" mean ? - Is that 2 cursor spaces of width ?
Much thanks in advance...
--
Thanks and regards,
Sudip Bhattacharya
Mobile: +91 9999 100 706
Home Land line: +91 11 22237561
Office Land line: +91 0124 4321078
eMail ID: sudipb at sudipb.com; sudipb at gmail.com
Please visit my website at: www.sudipb.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100612/1c4a3d34/attachment.html>
More information about the Tutor
mailing list