8.1 String operations
"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.
"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.
upper()
converts
"a"
to uppercase (
"A"
). When checking "a".upper() == "A"
, the output is True.
8.2 String slicing
"e"
.
"u"
is at index -2.
"e"
in the string
"chance"
has index of -1 (negative indexing) and 5 (positive indexing).
greeting[3:]
is the substring starting from index 3 up to the end of the string, which is
"Leila"
.
TypeError: 'str' object does not support item assignment
.
"o"
, is replacing the content of variable
str
. As such, the print statement prints
"o"
.
8.3 Searching/testing strings
for
loop iterates through the characters in
"string"
and prints all character without any separator character.
"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
.
"cab"
and prints each character's uppercase equivalent. Therefore, the output is
"CAB"
.
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.
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.
"ooo"
does not exist in the string
"ooo"
and hence the index()
method returns a ValueError
.
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
"Ana"
is substituted for the replacement field {} in the template and hence the output is
"Hello Ana!"
.
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.
"Bengio"
is substituted in the named replacement field associated with the tag name
; hence, the output is
"Hey Bengio!"
.
KeyError: 'name'
.
"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"
.
name
field to be 12, so len(formatted_name)
is
12
.
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.
{: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*2*3*"
is broken into substrings using the
'*'
delimiter, the output is ["1", "2", "3"]
.
"a year includes 12 months"
is broken into substrings using blank space characters, the list ["a", "year", "includes", "12", "months"]
is created.
elements
are concatenated together using a comma, so the output is
'A,beautiful,day,for,learning'
.
"12"
.