What is a String index?
The string index is a programming construct used to track the value of each individual element in a string
What do we call attaching two strings side by side?
What is concatenation?
True or False? This example prints Hello: print(“Hello”):
True
What does the following code output?
name = "Edgar Allan Poe"
pos = name.find(a)
3
What is a method?
A method is a programming construct that helps us change the behavior or properties of a variable or object
What will the following code print?
s = "Hello there!"
print(s[:100])
Error: IndexOutOfBounds
What property allows strings to be replaced but not modified
Immutability
What the following program print?
print(“c” in “bananas”)
False
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
What method returns a lowercase version of a string?
.lower()
What will be printed if we run the following lines?
my_string = “ABCDE”
print(my_string[-5])
A
What will the following code output?
greeting = "hi there!"
greeting [0] = "H"
print(greeting)
Error, strings are immutable
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
What can we do to find out if a variable is found at an index value in an element?
if variable in element
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
What will the following code output?
s = "hi there!"
print(s(-1))
Error
Type error: "str" object not callable on line 2
Generally speaking, how can we list each element of a string in a column?
Ex: "HI!"
H
I
!
for variable in element:
print(variable)
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
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
The method isupper() will cause my string to convert to all uppercase
False
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
What would this program print?
def mystery(num, pattern):
result = ""
for i in range(num):
result = result + pattern
return result
print(mystery(3, "@"))
@@@
What will be printed if we run the following lines?
my_string = “ABCDE”
print(my_string[:7])
Error: IndexOutOfBounds
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
What will the following code output?
my_string = "tomato"
index = my_string.find("veggie")
print(index)
-1