# File: strstuff.py # # Show how strings and string operations work # # Matt Bishop, MHI 289I, Fall 2024 # # read in a string # mystr = 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], end=' ') print("\nHere's another way:") index = 0 for i in mystr: print("(" + str(index+1) + ")", i, end=' ') index = index + 1 print("\n") 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], end=' ') print("\n") 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, end=' ') # # maybe it's a palindrome; let's check ... # if mystr == revmystr: print(" -- hey, it's a palindrome!", end=' ') print("\n") input("Type enter to continue> ") print(" ") # # let's count and list vowels # print("Let's count vowels: ", end=' ') 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", "%.2f" % ((vowelct/len(mystr))*100.0), "percent of the characters") print(" ") 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)