Here is the list of string methods (same link as the one on the homepage). One or more of these methods may be useful below...
1. Let's do a weird thing to a string: chop it up into 3-character segments, reverse each segment, and put it back together again. So, create a function called Reverse3(s), which will be given a non-empty string whose length is a multiple of 3 (like 3 characters, or 300 characters, etc., but not 4 characters, etc.), and will do that weird thing to it.
Reverse3('PQR') # returns 'RQP' Reverse3('abcdef123') # returns 'cbafed321'
2a. Create a function called ShiftRight(s,n) where s is a string, and n is any positive whole number less than len(s). It will shift each character in s, n positions to the right (and wrap around). So...
ShiftRight('abcdefg',2) # returns 'fgabcde' because each character has moved 2 positions to the right (and wrapped around at the end) ShiftRight('abcdefg',6) # returns 'bcdefga'
2b. Create the function ShiftLeft(s,n) which does the same thing as ShiftRight() but shifts the string to the left -- n is positive, but less than len(s).
ShiftLeft('abcd',1) # returns 'bcda'
2c. Create the function Shift(s,n) that shifts the string to the right if n > 0, and to the left if n < 0, and not at all if n == 0. TEST, TEST, TEST.
3a. We saw how Jules (we're on familiar terms with Julius Caesar) shifted the alphabet to create his cipher (encryption technique), by choosing an output character 3 steps before (left) the input character (i.e. "E" will be replaced by "B", etc.) Let's suppose Brutus (not the cartoon character) wants to do something similar, but instead wants have the output character 4 steps after (right) the input character, for all lower and upper-case letters (e.g. "A" will be replaced by "E", etc.). Brutus and Antony are working, in secret, on a speech and exchanging drafts of it in encrypted form. You intercept one of the transmissions from Antony to Brutus and see the following string:"Bneajzo, Nkiwjo, ykqjpnuiaj, hajz ia ukqn jkoao;"
What did Antony write? Copy the message into the Comments-to-Teacher
3b. (a little harder) Below, in encrypted form, is one of the most famous messages in cryptography. It has been encrypted using the shift-alphabet technique that we have been using above, but you don't know how many positions to the right or left. Create the function DecryptFamous(s) that will decrypt this message into plain English, knowing that one of the words that the message contains is "Magic". Hint: you might find the "find()" string-method useful -- figure out how to use it. Note: you may NOT try to solve this by hand...your Python function should do all of the work, and output the correct English translation. Once you've decrypted the quotation, look it up on the net to see why it's famous among cryptographers. Copy the message into the Comments-to-Teacher.
"Cqn Vjprl Fxamb jan Bzdnjvrbq Xbbroajpn"
Challenge Problems:
4. Create your own version of the find() string-method: MyFind(s,substring), that will be given a string s, and another string substring, and will try to find the position of the first occurrence of substring inside the string s. It will return the position of the first character in s that starts the string substring, or -1 if the substring cannot be found in s. Examples:
MyFind('Hi there','there') # returns 3 because "there" starts in position 3 inside "Hi there" MyFind('Hi there', Hit') # returns -1 MyFind('George or Fred','or') # returns 2 because that's the position of the first "or"
5. Create your own version of the strip() string-method, which removes all leading and trailing white space from a string. MyStrip(s) will return a copy of s having removed any leading spaces and also any trailing spaces. So:
MyStrip(" Hi there ") # will return "Hi there" MyStrip("Whatever") # will return "Whatever"