Lab: Simple Strings
Preparation
a. Make sure you are booted into the Mac OSX side of the computer.
b. Mount the COURSES network drive by double clicking the alias icon you created previously. If you were unable to set up the alias, you can follow the Course Directory Setup instructions to mount the drive and create the alias.
c. Open up a Terminal, navigate to your StuWork directory, create a folder named lab-09-21, and change your directory to the newly created folder. (Remember that you should replace USERNAME with your actual username!)
$ cd /Volumes/COURSES/cs111-03-f18/StuWork/USERNAME/
$ mkdir lab-09-21
$ cd lab-09-21
d. Open Brackets and make sure you have your StuWork/USERNAME directory opened. (You will see all of its subfolders in the left panel if so.) If the folder is not opened, you can open it by selecting File -> Open Folder and navigating to COURSES, then cs111-03-f18, then StuWork, then USERNAME.
Exercise 1
We’ve discussed how strings are sequences of characters. Is it possible to have an empty sequence?
a. Try typing type("") into the REPL to see if it still qualifies as a string.
b. What do you think len("") will evaluate to? Verify this in the REPL.
c. What do you think "" + "abc" will evaluate to? Verify this in the REPL.
d. What about "" * 5? Verify this in the REPL.
The value "" is called the empty string and we will encounter it many times throughout the term.
Let’s also play around with escaped characters.
e. Try typing len("\n") into the REPL. Is this what you expected?
f. Try typing print("hello\nworld") into the REPL. What was the character \n interpreted as?
g. Try typing print("Dear Friend,\n\nI am pleased to inform you that...") into the REPL.
What if we actually want to have a backslash character in our string?
h. Try typing len("\\") into the REPL.
i. Finally, try typing print("\\") into the REPL.
Whenever we type a backslash \ before another character, it escapes that character to mean something else. If we escape a backslash by typing \\, this actually only produces a single \ character in our string.
Exercise 2
Type the following into the REPL. (For the record, I found this word by searching for “long English words” in Google. This one in particular caught my eye.)
>>> my_string = "equanimity"
a. Write an expression that extracts the "u" character out of my_string.
b. Write an expression that extracts the "q" character out of my_string.
c. Write an expression that extracts the substring "equanimit" out of my_string. (i.e. just the last character is dropped)
d. Write an expression that extracts the substring "quanimity" out of my_string. (i.e. just the first character is dropped)
e. Write an expression that extracts the substring "equ" out of my_string.
f. Write an expression that extracts the substring "ity" out of my_string.
g. Write an expression that combines your expressions from c. and d. to produce "equity".
Exercise 3
a. What do you think will happen when you evaluate the following expression into the REPL?
>>> my_string[len(my_string)]
b. What do you think will happen if you evaluate the above expression for different strings my_string? Can you make a general statement about what will happen for any string my_string?
c. Evaluate the following two expressions and compare their results.
>>> my_string[len(my_string)-1]
>>> my_string[-1]
d. Evaluate the following two expressions and compare their results.
>>> my_string[len(my_string)-3]
>>> my_string[-3]
e. What do you think my_string[index] is doing for negative values of index? Discuss this with your partner and try it on other negative values such as -2 and -3.
Exercise 4
Write a program called print_middle_character.py that asks the user to type in a string and the program prints out what the “middle” character. For an odd-length string, the middle is obvious; for example, "b" is the middle character of "aaabccc". For an even-length string, your program should print the left middle; for example, it should print "o" when given "coat".
Exercise 5
We have seen that you can slice strings with the syntax my_string[start:end]. What do you think happens if you remove the start or the end index? Let’s try it.
First create a variable my_string = "abcdefg"
a. Try typing my_string[3:] into the REPL.
b. Try typing my_string[:4] into the REPL.
c. Experiment with leaving out start or end on a few more choices of indices and values of my_string.
d. When you leave off the start or end index, Python assumes some default value. What is the default start index? What is the default end index?
Exercise 6
Create a file named string_experiment.py that contains the following code.
# string_experiment.py
#
# Asks the user to enter a string, and then prints out the individual
# characters of that string
their_string = input("Please enter a string of your choice: ")
print("\nThe individual characters of your string are printed below.\n")
counter = 0 # counts how many times the loop has executed
for c in their_string:
counter = counter + 1 # increments the counter by one
print("Character", counter, "is:", c)
print("\nThe total number of times the loop executed is:", counter)
a. Run the code on a variety of strings to see how it works.
b. Is the counter actually necessary to how the loop works? Try replacing the loop and the counter with the following.
for c in their_string:
print("The next character is:", c)
c. Modify the code so that it it prints out each character in the string twice.
Exercise 7
Create a program called sum_digits.py that asks the user to enter an integer and it returns the sum of the individual digits of that integer.
Hint: Treat the input as a string and use the for loop structure from Exercise 6 to iterate over the individual characters, convert them to integers, and add them up. You may want to have a variable named sum similar to the counter variable, too.