Starting out with Python Chapter 8
If a string has 10 characters, what is the index of the last character?
9
What does the following code display? numbers = [1, 2, 3, 4, 5, 6, 7] print(numbers[4:6])
[5, 6]
The isupper method converts a string to all uppercase characters. (True or False)
False
When you call a string's split method, the method divides the string into two substrings. (True or False)
False
This will happen if you try to use an index that is out of range for a string.
IndexError
What happens if you try to use an invalid index to access a character in a string?
IndexError
What does the following code display? mystr = 'yes' mystr += 'no' mystr += 'yes' print(mystr)
yesnoyes
What is the index of the first character in a string?
0
This is the first index in a string.
0
What does the following code print out? full_name = 'Patty Lynn Smith' middle_name = full_name[6:10] print(middle_name)
Lynn
What is wrong with the following code? animal = 'Tiger' animal[0] = 'L'
The expression animal[0] cannot appear on the left side of an assignment operator.
This is the last index in a string.
The size of the string minus one.
An IndexError will occur if you tru to use an index that is out of range for a particular string. (True or False)
True
Invalid indexes do not cause slicing expressions to raise an exception. (True or False)
True
Once a string is created, it cannot be changed. (True or False)
True
The modulus operator can be used to find the remainder as well as format strings. (True or False)
True
The repetition operator ( *) works with strings as well as with lists. (True or False)
True
You can use the for loop to iterate over the individual characters in a string. (True or False)
True
What will the following code display? mystring = 'abcdefg' print(mystring[:3])
abc
What does the following code display? mystr = 'abc' * 3 print(mystr)
abcabcabc
What will the following code display? mystring = 'abcdefg' print(mystring[:])
abcdefg
What does the following code display? mystring = 'abcdefg' print(mystring[2:5])
cde
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
If the ____ index specifies a position beyond the end of the string, Python will use the length of the string instead.
end
Which method returns true if the string ends with substring?
endswith(substring)
This string method returns the lowest index in the string where a specified substring is found.
find()
Which method returns the lowest index in the string where substring is found? If the substring is not found, the method returns -1.
find(substring)
What does the %g operator format?
float
One of the easiest ways to access the individual characters in a string is to use the ___ loop.
for
This operator determines whether one string is contained inside another string.
in
What operator would you use to determine whether one string is contained in another string?
in
What does the %d operator format?
integer
Which method returns true if the string contains only alphabetic letters or digits and is at least one character?
isalnum()
This string method returns true if a string contains only alphabetic characters and is at least one character in length.
isalpha()
Which method returns true if the string contains only alphabetic letters and is at least one character length?
isalpha()
This string method returns true if a string contains only numeric digits and is at least one character in length.
isdigit()
Which method returns true if the string contains only numeric digits and is at least one character in length?
isdigit()
Which method returns true if all the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter?
islower()
Which method returns true if the string contains only whitespace characters and is at least one character in length? (whitespace characters are spaces, newlines \n, and tabs \t)
isspace()
Which method returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter>
isupper()
What does the following code display? name = 'joe' print(name.lower()) print(name.upper()) print(name)
joe JOE joe
How do you find the length of a string?
len()
This function returns the length of a string.
len()
Which method returns a copy of the string with all the letters converted to lowercase?
lower()
Which method returns a copy of the string with all leading whitespace characters removed?
lstrip()
Which method returns a copy of the string with all instances of char that appear at the beginning of the string removed?
lstrip(char)
Which method returns a copy of the string with all instances of old replaced by new?
replace(old, new)
This string method returns a copy of the string with all leading white space characters removed.
rstrip()
Which method returns a copy of the string with all trailing whitespace characters removed?
rstrip()
Which method returns a copy of the string with all instances of char that appear at the end of the string removed?
rstrip(char)
You can use _____________________ to select a range of characters from a string.
slicing expressions
If the _____ index is greater than the end index, the slicing expression will return an empty string.
start
If the _____ index specifies a position before the beginning of the string, Python will use 0 instead.
start
Which method returns true if the string starts with substring?
startswith(substring)
What does the %s operator format?
string
This string method returns a copy of the string with all leading and trailing whitespace characters removed.
strip()
Which method returns a copy of the string with all the leading and trailing whitespace removed?
strip()
Which method returns a copy of the string with all instances of char that appear at the beginning and end of the string removed?
strip(char)
A __________ is a string that appears within another string.
substring
String slices are also called __________.
substrings
Which method returns a copy of the string with all the letters converted to uppercase?
upper()