# File: strstuff.py # Some string stuff # # Matt Bishop, MHI 289I, Winter 2018 # # read in a string # mystr = raw_input("Please type some text: ") # # print the string out letter by letter, in two different ways # print "First, we print each character in order:" for i in range(len(mystr)): print "(" + str(i+1) + ")", mystr[i], print "\nHere's another way:" index = 0 for i in mystr: print "(" + str(index+1) + ")", i, index = index + 1 print "\n" raw_input("Type enter to continue> ") print " " # # same thing, but backwards # print "Next, we print each character in reverse order:" for i in range(len(mystr)): print "(" + str(i+1) + ")", mystr[-i-1], print "\n" raw_input("Type enter to continue> ") print " " # # now generate the reverse string # print "Now, let's reverse it as a string" revmystr = "" for i in range(len(mystr)): revmystr = revmystr + mystr[-i-1] print "And it is: ", revmystr, # # maybe it's a palindrome; let's check ... # if mystr == revmystr: print " -- hey, it's a palindrome!" else: print "" print "" raw_input("Type enter to continue> ") print " " # # let's count and list vowels # print "Let's count vowels: ", vowels = "aeiou" vowelct = 0 vowellist = "" for ch in mystr: if ch.lower() in vowels: vowelct = vowelct + 1 vowellist = vowellist + ch.lower() print "Out of", len(mystr), "characters, there are", vowelct, "vowels:", vowellist print "That's", float(vowelct)/float(len(mystr))*100.0, "percent of the characters" print "" raw_input("Type enter to continue> ") print " " # # now make a pyramid or two by chopping letters off from the outside in # # first, big to little # print "Now let's look at some slices:" print mystr for i in range(len(mystr) / 2 + len(mystr) % 2): if i == 0: continue print " "*(i-1), mystr[i:-i] # # last, little to big # for i in range(len(revmystr)/ 2 + len(revmystr) % 2, 0, -1): if i == len(revmystr) / 2 + len(revmystr) % 2: continue print " "*(i-1), revmystr[i:-i] print revmystr