Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.22 Functions and Strings Multiple Choice Questions
Checkpoint 5.22.1.
Q-1: Given the below code snippet, which of the following options would create a variable called final_string
that is assigned to the word “yelling”?
string1 = "yellow"
string2 = "screaming"
final_string = string1[:4] + string2[-3:]
Correct! Using the slice operator [start:end], the starting index is inclusive, while the ending index is exclusive.
final_string = string1[:4] + string2[6:]
Correct! Using the slice operator [start:end], the starting index is inclusive, while the ending index is exclusive.
final_string = string1[:3] + string2[6:]
Try again! This would assign final_string to "yeling".
final_string = string1[:3] + string2[6:-1]
Try again! This would assign final_string to "yelin".
final_string = string1[:4].append(string2[-3:])
Try again! String objects do not have the attribute "append".
Checkpoint 5.22.2.
Q-2: Which of the following choices correctly prints a sentence using the variable num_of_apples
?
print("I have " + num_of_apples + " apples.")
Try again! You can't concatenate a string and an integer together.
print("I have " + str("num_of_apples") + " apples.")
Try again! Do not enclose the variable name in a string or you will get just those characters.
print("I have 4 apples.")
Try again! This doesn't use the variable num_of_apples.
print("I have " + str(num_of_apples) + " apples.")
Correct! By using str, the integer variable is converted into a string.
Checkpoint 5.22.3.
Q-3: Which of the following are string methods?
split()
Correct!
lower()
Correct!
append()
Try again! This is a list method, not a string method.
startswith()
Correct!
sort()
Try again! This is a list method, not a string method.
Checkpoint 5.22.4.
Q-4: Which of the following code corresponds to the amount of characters in the following string
variable?
string = "I love coding!"
count(string)
Try again! Count is a python string method that counts the amount of occurrences of a substring.
len(string)
Correct!
int(string)
Try again! String with words cannot be converted to ints.
length(string)
Try again! Instead of length, it should be len, because length is not a built-in python function.
Checkpoint 5.22.5.
Q-5: What does the following code output?
def abbrev(first_name, last_name):
print(first_name[0:1] + ". " + last_name.lower())
abbrev("Joanne", "Weathers")
J. Weathers
Try again! The first letter in the last name should be lowercase.
Jo. Weathers
Try again! The end of a slice operator is exclusive (e.g., 1 is exclusive in this example).
oa. Weathers
Try again! Strings are indexed starting at 0.
J. weathers
Correct!
j. weathers
Try again! Only the last name should be lowercase.
Checkpoint 5.22.6.
Q-6: After running this code, what would be the output if the input was first_name = "Katie"
and last_name = "perkins"
?
def abbrev():
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
return("Hello " + first_name + last_name[0:2].capitalize() + ". ")
def main():
abbrev()
main()
Hello KatiePe.
Try again! This would be correct if there was a print statement.
Nothing.
Correct! Nothing would be outputted because there is no print statement.
Hello Katie Pe.
Try again! There shouldn't be a space between the first and last name.
Hello KatiePE.
Try again! The E should not be capitalized. The upper() method would capitalize the E, but not the capitalize() method.
Checkpoint 5.22.7.
Q-7: Given the variable item
, how would you grab the letters “tebo”?
def notebook():
item = "notebook"
# What goes here?
notebook()
print(item[2:7])
Try again! This would print "teboo".
print(item[2:6])
Correct!
print(item[-6:-2])
Correct!
print(item[3] + item[4] + item[5] + item[6])
Try again! This would print "eboo".
print(item[3:7])
Try again! This would print "eboo".