5.7.1 Strings Test

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which of the following expressions will get the last character in a string? Assume that word is a string variable. I. word[0] II. word[-1] III. word[len(word)] IV. word[len(word)-1]

II, IV

On which line does this program throw an error? word = "killer whale" pos = word.find(" ") word = word + "!" word[pos] = "-"

Line 4

What does this function do? # sentence: a string containing a sentence # char: a letter def mystery(sentence, char): res = "" for letter in sentence: if letter == " ": res = res + char else: res = res + letter return res

Returns the same sentence, but replaces every space with the letter char

What does "immutable" mean with respect to Python strings?

The variable can be assigned a new value, but the string's value cannot be modified.

What is printed out by this program? first = "abcde" second = "zyxwv" res = "" for i in range(len(first)): res = res + first[i] + second[i] print(res)

azbycxdwev

What is the output of the following program? sentence = "the dog" for letter in sentence: print(letter + letter)

tt hh ee dd oo gg

What function should be used in the blank to capitalize the first letter of the word stored in word? first_char = word[0] word = first_char.______() + word[1:]

upper

Which of the following if statements checks if the string variable sentence contains the word "the"? if "t" or "h" or "e" in sentence: print("Contains 'the'") if "the" in sentence: print("Contains 'the'") if "the" == sentence: print("Contains 'the'") if sentence in "the": print("Contains 'the'")

if "the" in sentence: print("Contains 'the'")

What is printed out by this program? word = "killer whale" print(word[0:100])

killer whale

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("z")

-1

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("Edsger")

0

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("W")

7

What does this function do? # address: string containing an email address def mystery(address): if "@" not in address: return "" address = address.strip() return address

Checks to make sure the email address is valid by checking for '@' in the address. Returns the address without any leading or trailing whitespace.

Which of the functions below will return a string that is in alternating caps? For example, alt_case("sheep") should return "sHeEp". def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res + word[i].upper() else: res + word[i].lower() return res def alt_case(word): res = "" return word.swapcase() def alt_case(word): res = "" for i in range(word): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

Write a function that takes one argument and prints out the total number of characters, digits, and special characters from a given string. For Example Given: srtl = "P@#yn26at^&i5ve" The Expected Outcome: Total counts of characters, digits, and symbols Characters = 8 Digits = 3 Symbol = 4 Hint: Use the following string functions isalpha(): To check if a string/character is an alphabet isdigit(): To check if a string/character is a digit.

def display_character_counts(s): character_count = 0 for ch in s: if ch.isalpha(): character_count = character_count + 1 digit_count = 0 for ch in s: if ch.isdigit(): digit_count = digit_count + 1 symbol_count = 0 for ch in s: if ch in "!@#$%^&*()./\\": symbol_count = symbol_count + 1 print("Characters = " + (character_count)) print("Digits = " + (digit_count)) print("Symbols = " + (symbol_count))

Which of the following functions would return the word "CAT" when given "iCjnyAyT"? def find_secret_word(message): hidden_word = "" for letter in message: if letter.upper(): hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in message: hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word def find_secret_word(message): hidden_word = "" for letter in message: if letter in message: hidden_word = hidden_word + letter return hidden_word

def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word

Which of the following choices will print AeCl? Assume the following variables have been defined. first_name = "Alice" last_name = "Carmichael" print(first_name[0] + last_name[0]) print(first_name[0] + last_name[0] + first_name[-1] + last_name[-1]) print(first_name[0] + first_name[-1] + last_name[0] + last_name[-1]) print(first_name[1:] + last_name[1:])

print(first_name[0] + first_name[-1] + last_name[0] + last_name[-1])

Which of the following expressions will result in "brown"? Let sentence = "The brown lazy dog" print(sentence[4:8]) print(sentence[4:9]) print(sentence[3:8]) print(sentence[3:9])

print(sentence[4:9])

Which of the following expressions will print "dog"? Let sentence = "lazy dog" print(sentence[6:]) print(sentence[:]) print(sentence[:5]) print(sentence[5:])

print(sentence[5:])

Which of the following expressions will print "L"? Let word = "PINEAPPLE" print(word[-4:]) print(word[-1]) print(word[-2]) print(word[-3])

print(word[-2])

Which of the following string operation is illegal? Assume that word = "music". word[0] = "M" word = word[2] + word[-1] word = "musical" word = word + "al"

word[0] = "M"


Ensembles d'études connexes

Project Risk Management Study Questions

View Set