# ShowFor.py # Charles Van Loan # February 9, 2015 """ Illustrates the use of for-loops in problems where it is necessary to traverse a string.""" def Reverse(s): """ Returns a string that is obtained from s by reversing the order of its characters. Precondition: s is a string.""" t ='' for c in s: t = c+t return t def nDigits(s): """ Returns an int whose value is the number of digit characters that are in s. Precondition: s is a string.""" n = 0; for c in s: # Increment n if c is a digit if c.isdigit(): n=n+1 return n if __name__ == '__main__': x = raw_input('Enter a string to reverse: ') y = Reverse(x) print '\n\n The reverse of "%s" is "%s"\n\n' % (x,y) x = raw_input('Enter a string with some digits: ') m = nDigits(x) print '\nThe string "%s" has %1d digit characters' %(x,m)