1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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 references 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 sring referenced by the variable mystring.
for letter in mystring:
if letter . isupper():
count += 1
Assume the 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(‘$’)