8.1 String operations
2.
b.
Since
"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.
3.
c.
While all characters in
"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.
6.
a.
The
upper()
converts
"a"
to uppercase (
"A"
). When checking "a".upper() == "A"
, the output is True.
8.2 String slicing
1.
b.
Since indexing starts from 0, the character at index 1 is the second character in the string, which is
"e"
.
2.
b.
Since negative indexing starts from -1 from the right of the string to the left,
"u"
is at index -2.
3.
a.
Character
"e"
in the string
"chance"
has index of -1 (negative indexing) and 5 (positive indexing).
6.
b.
The string slice of
greeting[3:]
is the substring starting from index 3 up to the end of the string, which is
"Leila"
.
8.
a.
Attempting to assign a value to an index or range of indexes in a string results in
TypeError: 'str' object does not support item assignment
.
9.
c.
The character at index 1,
"o"
, is replacing the content of variable
str
. As such, the print statement prints
"o"
.
8.3 Searching/testing strings
4.
a.
The
for
loop iterates through the characters in
"string"
and prints all character without any separator character.
5.
c.
The given code iterates over characters in string
"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
.
6.
c.
The code iterates over the characters in
"cab"
and prints each character's uppercase equivalent. Therefore, the output is
"CAB"
.
10.
a.
The
find()
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.
13.
a.
The
index()
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.
14.
c.
Substring
"ooo"
does not exist in the string
"ooo"
and hence the index()
method returns a ValueError
.
15.
a.
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"
.
8.4 String formatting
1.
c.
"Ana"
is substituted for the replacement field {} in the template and hence the output is
"Hello Ana!"
.
3.
a.
The arguments for the
format()
function are passed in order and will be substituted for the replacement fields. Additional arguments may be ignored by the template and remain unused.
4.
c.
"Bengio"
is substituted in the named replacement field associated with the tag name
; hence, the output is
"Hey Bengio!"
.
5.
b.
Since the string template uses a named argument, inputs are only accepted through the corresponding keys. Hence, the code generates
KeyError: 'name'
.
6.
b.
The greeting variable with value
"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"
.
7.
b.
The template specifies the minimum length for the
name
field to be 12, so len(formatted_name)
is
12
.
8.
c.
The alignment and the field length specifications format the
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.
9.
c.
The field length specification enforces the minimum length of the string. Inputs that are longer than the specified minimum length will not be modified.
11.
a.
The
{: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.
8.5 Splitting/joining strings
1.
b.
When
"1*2*3*"
is broken into substrings using the
'*'
delimiter, the output is ["1", "2", "3"]
.
2.
c.
When
"a year includes 12 months"
is broken into substrings using blank space characters, the list ["a", "year", "includes", "12", "months"]
is created.
4.
c.
Elements of the list
elements
are concatenated together using a comma, so the output is
'A,beautiful,day,for,learning'
.
6.
b.
Since the separator is an empty string, elements of the list will be concatenated without a separator and the output will be
"12"
.