Chapter 8

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

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

for letter in name:

  • print (letter)

2
New cards

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

0

3
New cards

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

9

4
New cards

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.

5
New cards

How do you find the length of a string?

Use the built-in len function.

6
New cards

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.

7
New cards

What will the following code display?

mystring = ‘abcdefg’

print (mystring[2:5])

cde

8
New cards

What will the following code display?

mystring = ‘abcdefg’

print(mystring[3: ])

defg

9
New cards

What will the following code display?

mystring = ‘abcdefg’

print(mystring[ :3])

abc

10
New cards

What will the following code display?

mystring = ‘abcdefg’

print(mystring[ : ])

abcdefg

11
New cards

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

if ‘d’ in mystring:

  • print (‘Yes, it is there.’)

12
New cards

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()

13
New cards

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’)

14
New cards

What is the output of the following code?

ch = ‘a’

ch2 = ch.upper()

print (ch, ch2)

a A

15
New cards

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) ‘)

16
New cards

What will the following code display?

var = ‘$’

print(var.upper())

$

17
New cards

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

18
New cards

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()

19
New cards

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(‘$’)