Chapter 8 Flashcards
Assume the variable name references a string. Write a for loop that prints each character in the string.
for letter in name:
print(letter)
What is the index of the first character in a string?
0
If a string has 10 characters, what is the index of the last character?
9
What happens if you try to use an invalid index to access a character in a string?
An IndexError exception will occur if you try to use an index that is out of range for a particular string.
How do you find the length of a string?
Use the built-in len function.
What is wrong with the following code?
animal = ‘Tiger’
animal[0] = ‘L’
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.
What will the following code display?
mystring = ‘abcdefg’
print(mystring[2:5])
cde
What will the following code display?
mystring = ‘abcdefg’
print(mystring[3:])
defg
What will the following code display?
mystring = ‘abcdefg’
print(mystring[:3])
abc
What will the following code display?
mystring = ‘abcdefg’
print(mystring[:])
abcdefg
Write code using the in operator that determines whether ‘d’ is in mystring.
if ‘d’ in mystring:
print(‘Yes, it is there.’)
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.
little = big.upper()
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.”
if ch.isdigit():
print(‘Digit’)
else:
print(‘No digit’)
What is the output of the following code?
ch = ‘a’
ch2 = ch.upper()
print(ch, ch2)
a A
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).
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) ‘)
What will the following code display?
var = ‘$’
print(var.upper())
$
Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable mystring.
for letter in mystring:
if letter.isupper():
count += 1
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’]
my_list = days.split()
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’]
my_list = values.split(‘$’)
This string method returns a copy of the string with all leading whitespace characters removed.
lstrip
my_string = “ Hello, world!”
stripped_string = my_string.lstrip()
print(stripped_string)
Output
Hello, world!
This string method returns the lowest index in the string where a specified substring is found.
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
This operator determines whether one string is contained inside another string.
in
main_string = “hello world”
substring = “world”
if substring in main_string:
print(“Substring found!”)
else:
print(“Substring not found!”)
This string method returns true if a string contains only alphabetic characters and is at least one character in length.
the isalpha method
This string method returns true if a string contains only numeric digits and is at least one character in length.
the isdigit method