Chapter 8 Flashcards

1
Q

Assume the variable name references a string. Write a for loop that prints each character in the string.

A

for letter in name:
print(letter)

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

What is the index of the first character in a string?

A

0

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

If a string has 10 characters, what is the index of the last character?

A

9

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

What happens if you try to use an invalid index to access a character in a string?

A

An IndexError exception will occur if you try to use an index that is out of range for a particular string.

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

How do you find the length of a string?

A

Use the built-in len function.

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

What is wrong with the following code?

animal = ‘Tiger’
animal[0] = ‘L’

A

The second statement attempts to assign a value to an individual character in the string.

Strings are immutable, however, so the expression animal[0] cannot appear on the left side of an assignment operator.

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

What will the following code display?

mystring = ‘abcdefg’
print(mystring[2:5])

A

cde

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

What will the following code display?

mystring = ‘abcdefg’
print(mystring[3:])

A

defg

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

What will the following code display?

mystring = ‘abcdefg’
print(mystring[:3])

A

abc

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

What will the following code display?

mystring = ‘abcdefg’
print(mystring[:])

A

abcdefg

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

Write code using the in operator that determines whether ‘d’ is in mystring.

A

if ‘d’ in mystring:
print(‘Yes, it is there.’)

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

Assume the variable big references a string. Write a statement that converts the string it references to lowercase and assigns the converted string to the variable little.

A

little = big.upper()

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

Write an if statement that displays “Digit” if the string referenced by the variable ch contains a numeric digit. Otherwise, it should display “No digit.”

A

if ch.isdigit():
print(‘Digit’)
else:
print(‘No digit’)

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

What is the output of the following code?

ch = ‘a’
ch2 = ch.upper()
print(ch, ch2)

A

a A

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

Write a loop that asks the user “Do you want to repeat the program or quit? (R/Q)”. The loop should repeat until the user has entered an R or Q (either uppercase or lowercase).

A

again = input(‘Do you want to repeat ‘ +
‘the program or quit? (R/Q) ‘)
while again.upper() != ‘R’ and again.upper() != ‘Q’:
again = input(‘Do you want to repeat the ‘ +
‘program or quit? (R/Q) ‘)

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

What will the following code display?

var = ‘$’
print(var.upper())

A

$

17
Q

Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable mystring.

A

for letter in mystring:
if letter.isupper():
count += 1

18
Q

Assume the following statement appears in a program:

days = ‘Monday Tuesday Wednesday’

Write a statement that splits the string, creating the following list:

[‘Monday’, ‘Tuesday’, ‘Wednesday’]

A

my_list = days.split()

19
Q

Assume the following statement appears in a program:

values = ‘one$two$three$four’

Write a statement that splits the string, creating the following list:

[‘one’, ‘two’, ‘three’, ‘four’]

A

my_list = values.split(‘$’)

20
Q

This string method returns a copy of the string with all leading whitespace characters removed.

A

lstrip

my_string = “ Hello, world!”
stripped_string = my_string.lstrip()
print(stripped_string)

Output
Hello, world!

21
Q

This string method returns the lowest index in the string where a specified substring is found.

A

find

my_string = “hello world”
index = my_string.find(“world”)
print(index) # Output: 6

my_string = “hello world”
index = my_string.find(“l”)
print(index) # Output: 2

22
Q

This operator determines whether one string is contained inside another string.

A

in

main_string = “hello world”
substring = “world”
if substring in main_string:
print(“Substring found!”)
else:
print(“Substring not found!”)

23
Q

This string method returns true if a string contains only alphabetic characters and is at least one character in length.

A

the isalpha method

24
Q

This string method returns true if a string contains only numeric digits and is at least one character in length.

A

the isdigit method

25
Q

This string method returns a copy of the string with all leading and trailing whitespace characters removed.

A

strip

my_string = “ Hello, world! “
stripped_string = my_string.strip()
print(stripped_string)

Output:

Hello, world!

26
Q

True or False Once a string is created, it cannot be changed.

A

True

27
Q

True or False You can use the for loop to iterate over the individual characters in a string.

A

True

my_string = “Hello”

for char in my_string:
print(char)

28
Q

True or False The isupper method converts a string to all uppercase characters.

A

False

upper() method converts a string to all uppercase

29
Q

True or False The repetition operator (*) works with strings as well as with lists.

A

True

30
Q

True or False When you call a string’s split method, the method divides the string into two substrings.

A

False

split() method in Python, the method divides the string into multiple substrings based on a specified separator