Strings Practice Quiz¶
To confirm that you understand the string concepts you’ve seen in Python, try to answer the following questions without opening Python.
Question 1¶
strings-quiz1: In the following code, how many lines does this code print?:
sentence = "python rocks"
print(sentence[5])
Question 2¶
strings-quiz2: In the following code, what does this code print?:
sentence = "python rocks"
print(sentence[2] + sentence[-3])
Question 3¶
strings-quiz3: In the following code, how many times is the word “Saskatchewan” printed?:
word = "practice"
for character in word:
print("Saskatchewan")
Question 4¶
strings-quiz4: In the following code, what does this code print?:
sentence = "Walter Murray Collegiate Institute"
print(sentence[8:12])
Question 5¶
strings-quiz5: In the following code, what does this code print?:
sentence = "Walter Murray Collegiate Institute"
print(sentence[8:12])
Question 6¶
strings-quiz6: In the following code, what does this code print?:
sentence = "Walter Murray Collegiate Institute"
print(sentence[:9])
Question 7¶
strings-quiz7: In the following code, what does this code print?:
sentence = "Greenall"
print(len(sentence))
Question 8¶
strings-quiz8: In the following code, what does this code print?:
place = "sask"
thing = ""
for item in place:
thing = thing + item
print(thing)
Question 9¶
strings-quiz9: In the following code, what does this code print?:
place = "sask"
thing = ""
for item in place:
thing = item + thing
print(thing)
Question 10¶
strings-quiz10: In the following code, what does this code print?:
def applyRule(character):
newCharacter = ""
if character == "X":
newCharacter = "X+YF+"
elif character == "Y":
newCharacter = "-FX-Y"
else:
newCharacter = character
return newCharacter
print(applyRule("Y"))
Question 11¶
strings-quiz11: In the following code, what does this code print?:
def applyRule(character):
newCharacter = ""
if character == "X":
newCharacter = "X+YF+"
elif character == "Y":
newCharacter = "-FX-Y"
else:
newCharacter = character
return newCharacter
def processString(aString):
newString = ""
for character in aString:
newString = newString + applyRule(character)
return newString
print(processString("XZY"))