Week 8 Flashcards

1
Q

You cannot use a for loop to iterate over the characters in a string.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Indexing works with both strings and lists.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The index -1 identifies the last character of a string.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The following expression is valid:
string[i] = ‘i’

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The following code will display ‘yes + no’:
mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr
print(mystr)

A

False
yesno

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If the + operator is used on strings, it produces a string that is a combination of the two strings used as its operands.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When accessing each character in a string, such as for copying purposes, you would typically use a while loop.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The strip() method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

0 through 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

c. yesnono

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.’

A

b. ‘1357’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

a. ‘ Country Ln.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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’

A

c. ‘Ln.’

17
Q

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’

A

c. ‘02468’

18
Q

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

A

c. greater than

19
Q

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

A

b. the string with all leading whitespaces removed

20
Q

What is the first negative index in a string?
a. 0
b. -1
c. -0
d. the size of the string minus one

A

b. -1

21
Q

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

a. ‘zzzzzzzzzzzzzzz’

22
Q

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)

A

b. find(substring)

23
Q

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

a. endswith(substring)

24
Q

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’]

A

b. [‘03’, ‘07’, ‘2018’]

25
Q

What will be the value of the variable string after the following code executes?
string = ‘abcd’
string.upper()
a. ‘abcd’
b. ‘ABCD’
c. ‘Abcd’
d. Nothing; this code is invalid

A

b. ‘ABCD’

26
Q

What will be the value of the variable string after the following code executes?
string = ‘Hello’
string += ‘ world!’
a. ‘Hello’
b. ‘ world!’
c. ‘Hello world!’
d. Nothing; this code is invalid

A

c. ‘Hello world!’

27
Q

What will display after the following code executes?
password = ‘ILOVEPYTHON’
if password.isalpha():
print(‘Invalid, must contain one number.’)
elif password.isdigit():
print(‘Invalid, must have one non-numeric character.’)
elif password.isupper():
print(‘Invalid, cannot be all uppercase characters.’)
else:
print(‘Your password is secure!’)
a. Invalid, must contain one number.
b. Invalid, must have one non-numeric character.
c. Invalid, must contain one number.
Invalid, cannot be all uppercase characters.
d. Your password is secure!

A

a. Invalid, must contain one number.

28
Q

Each character in a string has a(n) __________ which specifies its position in the string.

A

index

29
Q

A(n) __________ exception will occur if you try to use an index that is out of range for a particular string.

A

IndexError

30
Q

The isalpha() method returns __________ if the string contains only alphabetic characters and is at least one character in length.

A

true

31
Q

A(n) __________ is a span of characters that are taken from within a string.

A

slice

32
Q

When the operand on the left side of the * symbol is a string and the operand on the right side is an integer, the * becomes the ___________ operator.

A

repetition

33
Q

The third number in string slicing brackets represents the ___________ value.

A

step

34
Q

The __________ operator can be used to determine whether one string is contained in another string.

A

in

35
Q

The __________ method returns True if the string contains only numeric digits.

A

isdigit()

36
Q

The __________ method returns the list of the words in a string.

A

split()

37
Q

The __________ method returns a copy of the string with all the alphabetic letters converted to lower case.

A

lower()

38
Q

The ____ specifier is a special set of characters that specify how a value should be formatted.

A

formatting

39
Q

A(n) _____ character is a special character that is preceded with a backslash, appearing inside a string literal.

A

escape