# string-puzzle-df-soln.py # Lillian Lee (LJL2@cornell.edu) # Jan 29, 2013 #using import import string """Demonstrate putting a sequence of commands into a python file, and several solutions to one of the lecture problems. Given: info as specified in lecture Goal: df as specified in lecture """ # using an imported module's functions print 'recapitalized version of info:' + string.capwords(info) +':' # Let's use the example from lecture info = 'RAISMAN, 6.7, 9.1,0' # Solution presented in lecture startcomma = info.index(',') tail = info[startcomma+1:] # substring of info starting after 1st comma endcomma = tail.index(',') df = tail[:endcomma].strip() print 'first solution:' + df + ':' # the colons help expose extra spaces # Alternate, less preferable solution # Using strip is better - safeguards against the random spaces people type startcomma = info.index(',') tail = info[startcomma+2:] # substring of info starting after 1st comma and following space endcomma = tail.index(',') df = tail[:endcomma] print 'second solution:' + df + ':' # More compact but less readable solution df = info[info.find(',')+1:info.find(',',info.find(',')+1)].strip() print 'third solution:' + df + ':' # version with debugging prints # "str" converts its argument to a string # #print "start comma is:" + str(startcomma) + ":" #tail = info[startcomma+1:] #print "tail is:" + tail + ":" #endcomma = tail.index(',') #print "endcomma is:" + str(endcomma) + ":" #df = tail[:endcomma-1] #print 'in this solution:' + df + ':' # the colons help expose extra spaces