week8-worksheet
week8-worksheet
Name: EID:
Read the questions carefully, and answer each question in the space provided. Use scratch
paper to do your work and then copy your answers neatly and legibly onto the test paper.
Only answers recorded on the test paper will be graded.
1. (11 points: 1 point each) The following are true/false questions. Write either T
or F in the boxes at the bottom of page 1. If there’s any counterexample, it’s
false.
(b) If strings s1 and s2 are equivalent, the expression s1 in s2 will return False
because “in” checks for partial matches and not the complete operand length.
(c) In Python, you can directly modify a character within a string by using the
index notation, like s[2] = "z".
(e) Strings in Python are 0-based indexed (i.e., the first character is at index 0).
(f) The strip() method in Python removes both leading and trailing whitespace
from a string.
(g) Python allows you to compare strings using the relational operators
(<, <=, >, >=) based on their lengths.
(j) islower(), isdigit() and isalpha() are all functions, so they take a string
as a parameter rather than being directly called on a string.
a b c d e f g h i j k
Questions 2-9 are multiple choice. Each counts 2 points. Write the letter of the
BEST answer in the box on the next page. Please write your answer in
UPPERCASE. Each problem has a single answer.
A. 1 B. 2 C. 4 D. 5 E. -1
8. If you want to traverse through a string S using negative indices in Python, which
loop should you use?
A. for char in S
B. for i in range(len(S))
C. for i in range(-len(S), 0, -1)
D. for i in range(-len(S), 0)
E. for i in range(-1, -len(S)-1, -1)
F. C, D, and E
G. C and E
9. If s is a string, which expression will always evaluate to True?
A. chr(ord(s))) == s
B. s.upper().lower() == s
C. s[0:len(s)] == s
D. All of the above
2 3 4 5 6 7 8 9
The following 7 questions require you to trace the behavior of some Python code and
identify the output of that code. For each question, write the output for the code segment
on the provided line.
10. (3 points)
s = "vpduwFrrnlh"
new = ""
for char in s:
new += chr(ord(char) - 3)
print(new)
11. (3 points)
s = "SillyGoose-781"
result = ""
for char in s:
if char in str(not s):
result += "B"
elif char.isalpha():
result += char.upper()
elif char.isdigit():
result += str(int(char) + 5)
else:
result += char
print(result)
12. (3 points)
s = "oompaLoompa"
result = (s[-len(s):-len(s)+5] + " " + s[-6:])
print(result)
Page total: /9
CS303E Week 8 Worksheet: More on Strings 5
13. (3 points)
spongebob = "goofyG00ber"
patrick = "krus7ykr4b"
gary = "+25"
14. (3 points)
def catInTheHat(thing1, thing2):
thing1 = thing1 + thing2
thing2 = thing1
print(thing1 is thing2)
string1 = "greenEggs"
string2 = "Ham"
catInTheHat(string1, string2)
print(string1, string2)
15. (3 points)
myGuineaPig = "Jerry" # =)
print(myGuineaPig[0:1000] * 2)
16. (3 points)
alpha, beta = "castiel", "dean"
omega = beta[:2] + alpha[2:]
sigma = beta[:len(beta)] + alpha[:3]
print(min(omega, sigma), max(omega, sigma))