Week 8 Flashcards
You cannot use a for loop to iterate over the characters in a string.
False
Indexing works with both strings and lists.
True
In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.
True
Indexing of a string starts at 1 so the index of the first character is 1, the index of the second character is 2, and so forth.
False
The index -1 identifies the last character of a string.
False
The following expression is valid:
string[i] = ‘i’
False
The following code will display ‘yes + no’:
mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr
print(mystr)
False
yesno
If the + operator is used on strings, it produces a string that is a combination of the two strings used as its operands.
True
When accessing each character in a string, such as for copying purposes, you would typically use a while loop.
False
If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences in the paragraph.
True
The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.
True
What are the valid indexes for the string ‘New York’?
a. 0 through 7
b. 0 through 8
c. -1 through -8
d. -1 through 6
0 through 7
What will be displayed after the following code executes?
mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr * 2
print(mystr)
a. yes + no * 2
b. yes + no yes + no
c. yesnono
d. yesnoyesno
c. yesnono
What will be assigned to the variable s_string after the following code executes?
special = ‘1357 Country Ln.’
s_string = special[ :4]
a. ‘7’
b. ‘1357’
c. 5
d. ‘7 Country Ln.’
b. ‘1357’
What will be assigned to the variable s_string after the following code executes?
special = ‘1357 Country Ln.’
s_string = special[4:]
a. ‘ Country Ln.’
b. ‘1357’
c. ‘Coun’
d. ‘57 C’
a. ‘ Country Ln.’
What will be assigned to the variable s_string after the following code executes?
special = ‘1357 Country Ln.’
s_string = special[-3:]
a. ‘135’
b. ‘753’
c. ‘Ln.’
d. ‘y Ln’
c. ‘Ln.’
What will be assigned to the variable some_nums after the following code executes?
special = ‘0123456789’
some_nums = special[0:10:2]
a. ‘0123456789’
b. ‘24682468’
c. ‘02468’
d. ‘02020202020202020202’
c. ‘02468’
If the start index is __________ the end index, the slicing expression will return an empty string.
a. equal to
b. less than
c. greater than
d. less than or equal to
c. greater than
What is the return value of the string method lstrip()?
a. the string with all whitespaces removed
b. the string with all leading whitespaces removed
c. the string with all leading tabs removed
d. the string with all leading spaces removed
b. the string with all leading whitespaces removed
What is the first negative index in a string?
a. 0
b. -1
c. -0
d. the size of the string minus one
b. -1
What will be assigned to the string variable pattern after the following code executes?
i = 3
pattern = ‘z’ * (5 * i)
a. ‘zzzzzzzzzzzzzzz’
b. ‘zzzzz’
c. ‘z * 15’
d. Nothing; this code is invalid
a. ‘zzzzzzzzzzzzzzz’
Which method would you use to determine whether a certain substring is present in a string?
a. endswith(substring)
b. find(substring)
c. replace(string, substring)
d. startswith(substring)
b. find(substring)
Which method would you use to determine whether a certain substring is the suffix of a string?
a. endswith(substring)
b. find(substring)
c. replace(string, substring)
d. startswith(substring)
a. endswith(substring)
What list will be referenced by the variable list_strip after the following code executes?
my_string = ‘03/07/2018’
list_strip = my_string.split(‘/’)
a. [‘3’, ‘7’, ‘2018’]
b. [‘03’, ‘07’, ‘2018’]
c. [‘3’, ‘/’, ‘7’, ‘/’, ‘2018’]
d. [‘03’, ‘/’, ‘07’, ‘/’, ‘2018’]
b. [‘03’, ‘07’, ‘2018’]