Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 8: Strings
48 problems · 0 figuresConcepts in Practice 8.1.1 §8.1 source ↗
"aaa" < "aab")?- a.
True - b.
False
Answer
Truea's ASCII value is less than b's ASCII value, the output for "aaa" < "aab" is True.Concepts in Practice 8.1.2 §8.1 source ↗
"aa" < "a")?- a.
True - b.
False
Answer
False "a" is shorter than "aa" , and the prefix "aa" with length 1 equals the second operand ( "a" ), the shorter string is considered smaller. As such, "aa" < "a" evaluates to False.Concepts in Practice 8.1.3 §8.1 source ↗
"aples" in "apples")?- a.
undefined - b.
True - c.
False
Answer
False "aples" exist in "apples" , the characters are not sequential, so the string "aples" is not a substring of "apples" . Therefore, "aples" in "apples" returns False.Concepts in Practice 8.1.4 §8.1 source ↗
"aBbA".lower()?- a.
abba - b.
ABBA - c.
abbA
Answer
abba"aBbA".lower() converts all characters to lowercase, and the output is "abba" .Concepts in Practice 8.1.5 §8.1 source ↗
"aBbA".upper()?- a.
abba - b.
ABBA - c.
ABbA - d.
aBBA
Answer
ABBA"aBbA".upper() converts all characters to uppercase, and the output is "ABBA" .Concepts in Practice 8.1.6 §8.1 source ↗
"a".upper() == "A")?- a.
True - b.
False
Answer
Trueupper() converts "a" to uppercase ( "A" ). When checking "a".upper() == "A", the output is True.Concepts in Practice 8.2.1 §8.2 source ↗
"hello"?- a.
"h" - b.
"e" - c.
"o"
Answer
"e" "e" .Concepts in Practice 8.2.2 §8.2 source ↗
"Blue"?- a.
"e" - b.
"u" - c.
"l"
Answer
"u" "u" is at index -2.Concepts in Practice 8.2.3 §8.2 source ↗
word = "chance" print(word[-1] == word[5])
- a.
True - b.
False
Answer
True "e" in the string "chance" has index of -1 (negative indexing) and 5 (positive indexing).Concepts in Practice 8.2.4 §8.2 source ↗
a_string = "Hello world" print(a_string[2:4])
- a.
"el" - b.
"ll" - c.
"llo"
Answer
"ll"a_string[2:4] is a slice including the third and fourth characters, which is "ll" .Concepts in Practice 8.2.5 §8.2 source ↗
location = "classroom" print(location[-3:-1])
- a.
"ro" - b.
"oo" - c.
"oom"
Answer
"oo"[-3:-1] includes characters at index -3 and -2, which are "oo" .Concepts in Practice 8.2.6 §8.2 source ↗
greeting = "hi Leila" name = greeting[3:]
- a.
" Leila" - b.
"Leila" - c.
"ila"
Answer
"Leila"greeting[3:] is the substring starting from index 3 up to the end of the string, which is "Leila" .Concepts in Practice 8.2.7 §8.2 source ↗
- a.
x = "string" x[0] = "*"
- b.
x = "string" x = "*" + x[1:]
- c.
x = "string" x = "*" + x
Answer
x = "string" x = "*" + x[1:]
Concepts in Practice 8.2.8 §8.2 source ↗
string_variable = "example" string_variable[-1] = ""
- a.
TypeError - b.
IndexError - c.
NameError
Answer
TypeErrorTypeError: 'str' object does not support item assignment.Concepts in Practice 8.2.9 §8.2 source ↗
str = "morning" str = str[1] print(str)
- a.
TypeError - b.
m - c.
o
Answer
o "o" , is replacing the content of variable str . As such, the print statement prints "o" .Concepts in Practice 8.3.1 §8.3 source ↗
"a" in "an umbrella")?- a.
2 - b.
False - c.
True - d.
1
Answer
True "a" is a substring of "an umbrella" , the output of "a" in "an umbrella" is True .Concepts in Practice 8.3.2 §8.3 source ↗
"ab" in "arbitrary")?- a.
True - b.
False
Answer
False "ab" is not a substring of "arbitrary" ; hence, the output is False .Concepts in Practice 8.3.3 §8.3 source ↗
"" in "string")?- a.
True - b.
False
Answer
True "" is a substring of length 0 for "string" , the output is True .Concepts in Practice 8.3.4 §8.3 source ↗
for c in "string": print(c, end = "")
- a.
string
- b.
s t r i n g
- c.
s t r i n g
Answer
string
for loop iterates through the characters in "string" and prints all character without any separator character.Concepts in Practice 8.3.5 §8.3 source ↗
count = 0
for c in "abca":
if c == "a":
count += 1
print(count)- a.
0 - b.
1 - c.
2
Answer
2 "abca" , and each time character "a" is observed, a counter variable is increased by 1. Since "a" appears twice in "abca" , the output will be 2 .Concepts in Practice 8.3.6 §8.3 source ↗
word = "cab"
for i in word:
if i == "a":
print("A", end = "")
if i == "b":
print("B", end = "")
if i == "c":
print("C", end = "")- a.
cab - b.
abc - c.
CAB - d.
ABC
Answer
CAB "cab" and prints each character's uppercase equivalent. Therefore, the output is "CAB" .Concepts in Practice 8.3.7 §8.3 source ↗
aaa".count("a"))?- a.
True - b.
1 - c.
3
Answer
3"aaa".count("a") returns 3 , which is the number of occurrences of "a" in string "aaa" .Concepts in Practice 8.3.8 §8.3 source ↗
"weather".count("b"))?- a.
0 - b.
-1 - c.
False
Answer
0 "b" exists in "weather" ; hence, the output of "weather".count("b") is 0 .Concepts in Practice 8.3.9 §8.3 source ↗
"aaa".count("aa"))?- a.
1 - b.
2 - c.
3
Answer
2 "aa" exist in "aaa" , one starting from index 0 and one starting from index 1.Concepts in Practice 8.3.10 §8.3 source ↗
"banana".find("a")?- a.
1 - b.
3 - c.
5
Answer
1find() method returns the index for the first occurrence of the substring in the given string. The index for the first occurrence of "a" is 1.Concepts in Practice 8.3.11 §8.3 source ↗
"banana".find("c")?- a.
0 - b.
-1 - c.
ValueError
Answer
-1 "c" is not a substring in "banana" , the find() method returns -1.Concepts in Practice 8.3.12 §8.3 source ↗
"b".find("banana")?- a.
-1 - b.
0 - c.
ValueError
Answer
-1 "banana" is not a substring of "b" , -1 is returned.Concepts in Practice 8.3.13 §8.3 source ↗
"school".index("o")?- a.
3 - b.
4 - c.
-3
Answer
3index() method returns the index of the start of the first occurrence of a substring in the given string. The index of the first occurrence of "o" in "school" is 3.Concepts in Practice 8.3.14 §8.3 source ↗
"school".index("ooo")?- a.
3 - b.
4 - c.
ValueError
Answer
ValueError "ooo" does not exist in the string "ooo" and hence the index() method returns a ValueError.Concepts in Practice 8.3.15 §8.3 source ↗
sentence = "This is a sentence"
index = sentence.index(" ")
print(sentence[:index])- a.
"This" - b.
"This " - c.
"sentence"
Answer
"This"sentence.index(" ") returns the index of the first space character. The print statement prints the prefix of the sentence ending just before the first space character, so the output is "This" .Concepts in Practice 8.4.1 §8.4 source ↗
print("Hello {}!".format("Ana"))?- a.
Ana - b.
Hello Ana - c.
Hello Ana!
Answer
Hello Ana! "Ana" is substituted for the replacement field {} in the template and hence the output is "Hello Ana!" .Concepts in Practice 8.4.2 §8.4 source ↗
print("{}:{}".format("One", "1"))?- a.
One1 - b.
One:1 - c.
1:One
Answer
One:1 "{}:{}" that outputs "One:1" .Concepts in Practice 8.4.3 §8.4 source ↗
print("{}".format("one", "two", "three"))?- a.
one - b.
two - c.
onetwothree
Answer
oneformat() function are passed in order and will be substituted for the replacement fields. Additional arguments may be ignored by the template and remain unused.Concepts in Practice 8.4.4 §8.4 source ↗
print("Hey {name}!".format(name = "Bengio"))?- a.
Hey name! - b.
Hey Bengio - c.
Hey Bengio!
Answer
Hey Bengio! "Bengio" is substituted in the named replacement field associated with the tag name; hence, the output is "Hey Bengio!" .Concepts in Practice 8.4.5 §8.4 source ↗
print("Hey {name}!".format("Bengio"))?- a.
Hey name! - b.
KeyError - c.
Hey Bengio!
Answer
KeyErrorKeyError: 'name'.Concepts in Practice 8.4.6 §8.4 source ↗
greeting = "Hi"
name = "Jess"
print("{greeting} {name}".format(greeting = greeting, name = name))- a.
greeting name - b.
Hi Jess - c.
Jess Hi
Answer
Hi Jess "Hi" is substituted for the greeting replacement field. The same is also done for the name replacement field and the name variable. The output is "Hi Jess" .Concepts in Practice 8.4.7 §8.4 source ↗
template = "{name:12}"
formatted_name = template.format(name = "Alice")
print(len(formatted_name))- a.
5 - b.
12 - c.
"Alice"
Answer
12name field to be 12, so len(formatted_name) is 12 .Concepts in Practice 8.4.8 §8.4 source ↗
template = "{greeting:>6}"
formatted_greeting = template.format(greeting = "Hello")
print(formatted_greeting[0])- a.
H - b.
" Hello" - c.Space character
Answer
greeting field to be six characters in length and right-aligned. Therefore, the variable formatted_greeting will take value " Hello" , and character 0 of the string is a space character.Concepts in Practice 8.4.9 §8.4 source ↗
template = "{:5}"
print(template.format("123456789"))- a.
56789 - b.
123456 - c.
123456789
Answer
123456789Concepts in Practice 8.4.10 §8.4 source ↗
print('{0:.3f}'.format(3.141592))?- a.
3.141592 - b.
3.1 - c.
3.142
Answer
3.142 .3 format specification shows that the output precision must be three decimal places.Concepts in Practice 8.4.11 §8.4 source ↗
print('{:1>3d}'.format(3))?- a.
113 - b.
311 - c.
3.000
Answer
113{:1>3d} specification outputs a 3-digit number in the output, and if the given argument does not have three digits, the number will be padded with 1's from the left side.Concepts in Practice 8.4.12 §8.4 source ↗
print('{:+d}'.format(123))?- a.
123 - b.
+123 - c.
:+123
Answer
+123{:+d} formats the decimal number by adding the positive sign.Concepts in Practice 8.5.1 §8.5 source ↗
print("1*2*3*".split('*'))?- a.
["1", "*", "2", "*", "3", "*"] - b.
["1", "2', "3"] - c.
[1, 2, 3]
Answer
["1", "2', "3"] "1*2*3*" is broken into substrings using the '*' delimiter, the output is ["1", "2", "3"].Concepts in Practice 8.5.2 §8.5 source ↗
print("a year includes 12 months".split())?- a.
["a year includes 12 months"] - b.
["a", "year", "includes", 12, "months"] - c.
["a", "year", "includes", "12", "months"]
Answer
["a", "year", "includes", "12", "months"] "a year includes 12 months" is broken into substrings using blank space characters, the list ["a", "year", "includes", "12", "months"] is created.Concepts in Practice 8.5.3 §8.5 source ↗
s = """This is a test""" out = s.split() print(out)
- a.
Error - b.
['This', 'is', 'a', 'test'] - c.
>['This', 'is a', 'test']
Answer
['This', 'is', 'a', 'test']['This', 'is', 'a', 'test'].Concepts in Practice 8.5.4 §8.5 source ↗
elements = ['A', 'beautiful', 'day', 'for', 'learning']
print(",".join(elements))- a.
'A beautiful day for learning' - b.
['A, beautiful, day, for, learning'] - c.
'A,beautiful,day,for,learning'
Answer
'A,beautiful,day,for,learning'elements are concatenated together using a comma, so the output is 'A,beautiful,day,for,learning' .Concepts in Practice 8.5.5 §8.5 source ↗
"sss".join(["1","2"])?- a.2
- b.5
- c.8
Answer
"sss".join(["1","2"]) is "1sss2" , which has a length of 5.Concepts in Practice 8.5.6 §8.5 source ↗
out? s = ["1", "2"] out = "".join(s)
- a.
12 - b.
"12" - c.
"1 2"
Answer
"12" "12" .Loading chapter 9…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.