Strings Week1

.docx

School

Grand Rapids Community College *

*We aren’t endorsed by this school

Course

117

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

3

Uploaded by ConstableWildcatMaster401

Report
Congratulations! You passed! Grade received 71.42% Latest Submission Grade 71.43% To pass 70% or higher Go to next item 1. Question 1 Which of the expressions below select the last character in the string "coconut" "coconut" ? 1 / 1 point "coconut"[-1] "coconut"[-1 ] Correct This expression selects the last character. "coconut"[6] "coconut"[6] Correct This expression selects the last character. "coconut"[-6] "coconut"[-6] "coconut"[7] "coconut"[7] 2. Question 2 Which of the string slices below selects the string "Sir" "Sir" from the string "Sir Robin" "Sir Robin" ? 1 / 1 point "Sir Robin"[: 3] "Sir Robin"[: 3] Correct This expression returns the string "Sir" "Sir" . "Sir Robin"[0 : 3] "Sir Robin"[0 : 3] Correct This expression returns the string "Sir" "Sir" . "Sir Robin"[1 : 4] "Sir Robin"[1 : 4] "Sir Robin"[1 : 3] "Sir Robin"[1 : 3] 3. Question 3 Which one of the operators below can not be used with strings in Python? 1 / 1 point * * (multiplication) + + (addition) - - (subtraction) in in (membership) Correct String subtraction in not defined in Python. 4. Question 4 What does the expression a_str.find(sub) a_str.find(sub) do when the string sub sub is not a substring of the string a_str a_str ? 1 / 1 point Raise an error. Return the value 0 0 .
Return the value None None . Return the value -1 -1 . Correct 5. Question 5 Which of the string format expressions below return the string "abracadabra" "abracadabra" ? 0 / 1 point "{}{}{}".format("abra", "cad", "abra") "{}{}{}".format("abra", "cad", "abra") "{0}{0}{0}".format("abra", "cad", "abra") "{0}{0}{0}".format("abra", "cad", "abr a") "{0}{1}{0}".format("abra", "cad") "{0}{1}{0}".format("abra", "cad") Correct This example returns "abracadabra" "abracadabra" . "{1}{2}{3}".format("abra", "cad", "abra") "{1}{2}{3}".format("abra", "cad", "abr a") This should not be selected This example raises an index error. 6. Question 6 Write a function count_vowels(word) count_vowels(word) that takes the string word word as input and returns the number of occurrences of lowercase vowels (i.e. the lowercase letters "aeiou" "aeiou" ) in word word . Hint: Python has a built-in string method that can count the number of occurrences of a letter in a string. After you have implemented count_vowels count_vowels , run the following two statements: 1 2 print (count_vowels( "aaassseefffgggiiijjjoOOkkkuuuu" )) print (count_vowels( "aovvouOucvicIIOveeOIclOeuvvauouuvciOIsle" )) The first statement should print 13 13 in the console. Enter the second number printed in the console in the box below. 1 / 1 point 17 Correct 7. Question 7 Write a function demystify(l1_string) demystify(l1_string) that takes a string composed of the characters "l" "l" and "1" "1" and returns the string formed by replacing each instance of "l" "l" by "a" "a" and each instance of "1" "1" by "b" "b" . Once you have implemented demystify demystify , test your function with calls below. 1 2 print (demystify( "lll111l1l1l1111lll" )) print (demystify( "111l1l11l11lll1lll1lll11111ll11l1ll1l111" ))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help