Chapter 8 Checkpoint Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will the following code display? mystring = 'abcdefg' print(mystring[3:])

defg

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

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

0

What will the following code display? var = '$' print(var.upper())

$

What is wrong with the following code? animal = 'Tiger' animal[0] = 'L'

Strings are immutable so since animal has been created it cannot be changed.

What is the output of the following code? ch = 'a' ch2 = ch.upper( ) print(ch, ch2)

a, A

What will the following code display? mystring = 'abcdefg' print(mystring[:])

abcdefg

What will the following code display? mystring = 'abdefg' print(mystring[:3])

abd

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 of lowercase)

answer = 'r', 'q', 'R', 'Q' while answer == 'r', 'R' print("Do you want to repeat the program or quit? (R/Q)." if answer == 'q', 'Q' print('end')

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.

big = 'a string' big.upper( ) little= big.lower( )

What will the following code display? mystring = 'abcdefg' print(mystring[2:5])

cde

Write a code using the in operator that determines whether 'd' is in mystring.

if 'd' in mystring: print('its there') else: print("d not found")

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

How do you find the length of a string?

len(string)

Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable mystring.

mystring = 'ABcDe' while mystring.upper( ) = True: print(mystring)

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

name= 'lame' for ch in name: print(ch)

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']

values = 'one$two$three$four' newlist = values.split( ) print(newlist)


Conjuntos de estudio relacionados

Introduction to Macroeconomics (Chapter 5: Long Run Economic Growth)

View Set

Maternity: FINAL chapters 1,2,3,10,11,12,19,20,21,13,14, 5,6,7,8,9,22

View Set