Indexing + Slicing
String Properties
What will the following code output?
Kitchen Sink
String Methods
100

What is a String index?

The string index is a programming construct used to track the value of each individual element in a string

100

What do we call attaching two strings side by side? 

What is concatenation?

100

True or False? This example prints Hello: print(“Hello”):

True

100

What does the following code output?

name = "Edgar Allan Poe"

pos = name.find(a)

3

100

What is a method?

A method is a programming construct that helps us change the behavior or properties of a variable or object

200

What will the following code print?

s = "Hello there!"

print(s[:100])

Error: IndexOutOfBounds

200

What property allows strings to be replaced but not modified

Immutability

200

What the following program print?

print(“c” in “bananas”)

False

200

Using str which example is the proper way to enable a float to be used under a string using the variable x =2.5? 

(a) float(str(x)) 

(b) str(float(x)) 

(c) str(float(int(x)))

B

200

What method returns a lowercase version of a string?

.lower()

300

What will be printed if we run the following lines?

my_string = “ABCDE”

print(my_string[-5])

A

300

What will the following code output?

greeting = "hi there!"

greeting [0] = "H"

print(greeting)

Error, strings are immutable

300

How many of these examples print?: 

(a) print(“world”) 

(b) print(“I am here” to have “fun”) 

(c) print(“It's a good day to play jeopardy!”)

2

300

What can we do to find out if a variable is found at an index value in an element?

if variable in element

300

What will be the output of the following code :

my_string = “ABCDE”

for letter in range(len(my_string)):

print(letter)

0

1

2

3

4

400

What will the following code output?

s = "hi there!"

print(s(-1))

Error

Type error: "str" object not callable on line 2

400

Generally speaking, how can we list each element of a string in a column?

Ex: "HI!"

H

I

!

for variable in element:

      print(variable)

400

What is the output of this code?

first = "abc"
second = "123"

res = ""
for i in range(len(first)):
    res = res + first[i] + second[i]
print(res)

a1b2c3

400

What is the result of this code?

# computes the surface area of a rectangular prism
def surface_area(length, width, height):
    return 2*(length * width + width * height + height * length)

print(surface_area(2,2,2))

24

400

The method isupper() will cause my string to convert to all uppercase

False

500

On which line does this program throw an error?

1.   word = "I like Python!"
2.   pos = word.find(" ")
3.   word = word + "!"
4.   word[pos] = "-"

Line 4 

500

What would this program print?

def mystery(num, pattern):
    result = ""
    for i in range(num):
        result = result + pattern
    return result

print(mystery(3, "@"))

@@@

500

What will be printed if we run the following lines? 

my_string = “ABCDE”

print(my_string[:7])

Error: IndexOutOfBounds

500

What will this code output?

word = "class"

def alt_case(word):
    res = ""
    for i in range(len(word)):
        if i % 2 == 1:
            res = res + word[i].upper()
        else:
            res = res + word[i].lower()            
    return res

cLaSs

500

What will the following code output?

my_string = "tomato"

index = my_string.find("veggie")

print(index)

-1

M
e
n
u