Questions from text chapter 9 (strings)
s = "python rocks" idx = 1 while idx < len(s): print(s[idx]) idx = idx + 2
(A) 0 * Idx=1 means start at the second character "y" in "python rocks" print(s[idx]) prints y idx= idx+2 prints every variable two characters from "y"
Evaluate the following comparison: "Dog" < "Doghouse" (A) True (B) False
(A) True
What is printed by the following statements? s = "python" excl = "!" print(s+excl*3) (A) python!!! (B) python!python!python! (C) pythonpythonpython! (D) Error, you cannot perform concatenation and repetition at the same time.
(A) python!!!
What is printed by the following statements? s = "python rocks" print(s[7:11] * 3) (A) rockrockrock (B) rock rock rock (C) rocksrocksrocks (D) Error, you cannot use repetition with slicing.
(A) rockrockrock
What is printed by the following statements? s = "python rocks" print(s[2] + s[-5]) (A) tr (B) ps (C) nn (D) Error, you cannot use the [ ] operator with the + operator.
(A) tr
What is printed by the following statements? s = "python rocks" print(s[1] * s.index("n")) (A) yyyyy (B) 55555 (C) n (D) Error, you cannot combine all those things together.
(A) yyyyy * s[1] is y and the index of n is 5, so 5 y characters
What is printed by the following statements? s = "python rocks" print(len(s)) (A) 11 (B) 12
(B) 12 * characters start at 1 in lens, all inclusive
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO") (A) 4 (B) 5 (C) 6 (D) Error, the for statement cannot use slice.
(B) 5
Evaluate the following comparison: "dog" < "Doghouse" (A) True (B) False
(B) False * The length does not matter. Lower case d is greater than upper case D.
Evaluate the following comparison: "dog" < "Dog" (A) True (B) False (C) They are the same word
(B) False *upper case is less than lower case according to the ordinal values of the characters
What is printed by the following statements? s = "python rocks" print(s[3]) (A) t (B) h (C) c (D) Error, you cannot use the [ ] operator with a string
(B) h * starts at 0
What is printed by the following statements? s = "python rocks" print(s[len(s)-5]) (A) o (B) r (C) s (D) Error, len(s) is 12 and there is no index 12.
(B) r * len(s) is 12 and 12-5 is 7. Use 7 as index and remember to start counting with 0
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s: print("HELLO") (A) 10 (B) 11 (C) 12 (D) Error, the for statement needs to use the range function.
(C) 12
How many times is the letter o printed by the following statements? s = "python rocks" for idx in range(len(s)): if idx % 2 == 0: print(s[idx]) (A) 0 (B) 1 (C) 2 (D) Error, the for statement cannot have an if inside.
(C) 2 * function prints: p t o (space) o k * This is because 2%2==0 (no remainder), so you skip over every 2 letters, starting with first letter
What is printed by the following statements? s = "python rocks" print(s.count("o") + s.count("p")) (A) 0 (B) 2 (C) 3
(C) 3 * count prints number of occurrences of the character
What is printed by the following statements: s = "Ball" s[0] = "C" print(s) (A) Ball (B) Call (C) Error
(C) Error
What is printed by the following statements: s = "ball" r = "" for item in s: r = item.upper() + r print(r) (A) Ball (B) BALL (C) LLAB
(C) LLAB * BALL is backwards because r comes after item.upper()
What is printed by the following statements? s = "python rocks" print(s[3:8]) (A) python (B) rocks (C) hon r (D) Error, you cannot have two numbers inside the [ ].
(C) hon r
What is printed by the following statements? s = "python" t = "rocks" print(s + t) (A) python rocks (B) python (C) pythonrocks (D) Error, you cannot add two strings together.
(C) pythonrocks
string comparisons
1. "zebra" comes after "banana" bc z comes after b 2. prints true or false
def count(text, aChar): lettercount = 0 for c in text: if c == aChar: lettercount = lettercount + 1 return lettercount print(count("banana","a"))
3
food = "banana bread" print(food.capitalize()) print("*" + food.center(25) + "*") print("*" + food.ljust(25) + "*") # stars added to show bounds print("*" + food.rjust(25) + "*") print(food.find("e")) print(food.find("na")) print(food.find("b")) print(food.rfind("e")) print(food.rfind("na")) print(food.rfind("b")) print(food.index("e"))
Banana bread * banana bread * *banana bread * * banana bread* 9 2 0 9 4 7 9 - just: returns string in field of white space - rjust: returns string in field of white on the right of the string - find: returns leftmost place where a character is found - rfind: returns rightmost place where a character is found - index: same as find, but returns a runtime error if character not found
Not (7>3)
False
Not(True or false)
False
True and False
False
for achar in "Go Spot Go": print(achar)
G o S p o t G o
greeting = "Hello, world!" newGreeting = 'J' + greeting[1:] print(newGreeting) print(greeting)
Jello, world! Hello, world!
singers = "Peter, Paul, and Mary" print(singers[0:5]) print(singers[7:11]) print(singers[17:21])
Peter Paul Mary * start at 0, include one space after word bc up to, but does not include
print('p' in 'apple') print('i' in 'apple') print('ap' in 'apple') print('pa' in 'apple')
True False True False
print('a' in 'a') print('apple' in 'apple') print('' in 'a') print('' in 'apple') print('x' not in 'apple')
True True True True True
fruit = "apple" for idx in range(len(fruit)): print(fruit[idx])
a p p l e * each loop (of 5) prints one letter
fruit = "apple" position = 0 while position < len(fruit): print(fruit[position]) position = position + 1
a p p l e * similar to for loop, set position to 0 to initialize and start at "a" in "apple", and then set it to 1 to set up with len function
def removeVowels(s): vowels = "aeiouAEIOU" sWithoutVowels = "" for eachChar in s: if eachChar not in vowels: sWithoutVowels = sWithoutVowels + eachChar return sWithoutVowels print(removeVowels("compsci")) print(removeVowels("aAbEefIijOopUus"))
cmpsc bfjps * initialize what you are using to compare characters with = 0 or = "" *in s because s represents parameter that holds word
In a string, the + represents concatenation (linking two operands)
fruit = "banana" bakedGood = " nut bread" # need space before nut so there is a space between banana and bread print(fruit + bakedGood) : banana nut bread
Strings are immutable
means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.
True or false is:
true