# Answers to class exercises on strings A = "Now is the time to panic" #pos:012345678901234567890123456789 #1. retrieving "the" print A[7:10] #2. creating "panic time" print A[19:]+A[10:15] #3. create Reverse(s) (equivalent to s[::-1] def Reverse(s): answer = '' i = len(s)-1 while i >= 0: answer += s[i] i -= 1 return answer # test #3: print Reverse('abcde') #4. Palindrome... Teddy="A MAN A PLAN A CANAL PANAMA" print Reverse(Teddy) #5. Replace 'hp' with 'Harry Potter' and 'hpc' with 'Hewlett-Packard' assuming # that they're surrounded by spaces' def HP_Replace(s): s = s.replace(' hp ', ' Harry Potter ') s = s.replace(' hpc ', ' Hewlett-Packard ') return s # test #5: print HP_Replace(' hp nearly destroyed hpc today when his wand touched a side of hpc ')