LESSON 03 – STRING MANIPULATION

 


WORK

PROGRAMS

 

QUESTION #1

Ask the user for a word and store it in a variable called word.  Write the statement(s) that will do the following:

 

a)     Output to screen the first character of word.

b)     Output to screen all characters from word except the first letter.

c)     Output to screen the last two characters of the string word.

d)     Output word all capitalized to screen.

e)     Output word with the first letter lower case and the rest in upper case. (Challenging)

f)      Output the index of the first occurrence of the letter a in word.

g)     Output the index of the second occurrence of the letter a in word. (Challenging)

h)     Output word without the first occurrence of a in it. (Challenging)

 

QUESTION #2

The following is a hidden message:

 

67 65 77 80 79
82 79 67 75 83

 

Thankfully, each number simply represents an ASCII character.  Use the chr function in python to decipher the message.

 

QUESTION #3
Create your own hidden message.  What would be the best strategy to create it?

 

QUESTION #4

Write a program that asks the user for a lower case letter and outputs that letter in its upper case version.

 

QUESTION #5A

Write a function that gets a single character and a shift_amount and then returns that character shifted by shift_amount.

 

Example:

print(shift("a", 3)) would print d to screen.

 

QUESTION #5B

Alter your function from part A to include a wrapping around the alphabet.  For example, if we shift the letter z by one, it should go to a.  Or, if we shift B by -4, it should go to X.

 

QUESTION #5C
Create a function called shift_word that gets a word and a shift_amount as arguments and then returns the new word with each character shifted by shift_amount.  This function should make use of your function created in the previous questions.