Python Chapter 8 CheckPoints
What will the following code display? var = '$' print(var.upper( ) )
$
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 in string?
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 range for a prticular string.
How do you find the length of a string?
Use the built-in len function.
What is the out put of the following code? ch = 'a' ch2 = ch.upper ( ) print (ch, ch2)
a A
What will the following code display? mystring = 'abcdefg' print (mylist [3:])
defg
Write a loop that counts the number of uppercase characters that appear in the string referenced by the varibale mystring.
for letter in mystring: if letter.isupper( ): count += 1
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 wrong with the following code? animal = 'Tiger' anmail [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[:3])
abc
What will the following code display? mystring = 'abcdefg' print (mystring[:])
abcdefg
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 ('Dyou 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? mystring = 'abdcefg' print (mystring[2:5])
cde
Write code using the in operator that determines whether 'd' is in mystring.
if 'd' in mystring: print('Yes, it is there.')
Write an if statement that displays "Digit" if the string refernced by the variable ch contains a numeric digit. Otherwise, it should display "No digit"
if ch.isdigit( ): print('Digit') else: print ('No digit')
Assume the variable big references a string. Write a statement that converts the string if references to lowercase and assigns the converted string to lthe variable little.
little =big.upper( )
Assume the following statement appears in a program: day = '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('$')