like another person above, I said 'Wooww !!' when I discovered enumerate()
I sang a praise to Python when I discovered repr() that gave me possibility to see precisely the content of strings that I wanted to analyse with a regex
I was very satisfied to discover that print '\n'.join(list_of_strings) is displayed much more rapidly with '\n'.join(...) than for ch in list_of_strings: print ch
splitlines(1) with an argument keeps the newlines
These four "tricks" combined in one snippet very useful to rapidly display the code source of a web page , line after line, each line being numbered , all the special characters like '\t' or newlines being not interpreted, and with the newlines present:
import urllib
from time import clock,sleep
sock = urllib.urlopen('http://docs.python.org/')
ch = sock.read()
sock.close()
te = clock()
for i,line in enumerate(ch.splitlines(1)):
print str(i) + ' ' + repr(line)
t1 = clock() - te
print "\n\nIn 3 seconds, I will print the same content, using '\\n'.join(....)\n"
sleep(3)
te = clock()
# here's the point of interest:
print '\n'.join(str(i) + ' ' + repr(line)
for i,line in enumerate(ch.splitlines(1)) )
t2 = clock() - te
print '\n'
print 'first display took',t1,'seconds'
print 'second display took',t2,'seconds'
With my not very fast computer, I got:
first display took 4.94626048841 seconds
second display took 0.109297410704 seconds