Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 8: Strings

48 problems · 0 figures

Concepts in Practice 8.1.1 §8.1 source ↗

What is the output of ("aaa" < "aab")?
  1. a.
    True
  2. b.
    False

Answer

A. True
Since a'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 ↗

What is the output of ("aa" < "a")?
  1. a.
    True
  2. b.
    False

Answer

B. False
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.

Concepts in Practice 8.1.3 §8.1 source ↗

What is the output of ("aples" in "apples")?
  1. a.
    undefined
  2. b.
    True
  3. c.
    False

Answer

C. False
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.

Concepts in Practice 8.1.4 §8.1 source ↗

What is the output of "aBbA".lower()?
  1. a.
    abba
  2. b.
    ABBA
  3. c.
    abbA

Answer

A. abba
"aBbA".lower() converts all characters to lowercase, and the output is "abba" .

Concepts in Practice 8.1.5 §8.1 source ↗

What is the output of "aBbA".upper()?
  1. a.
    abba
  2. b.
    ABBA
  3. c.
    ABbA
  4. d.
    aBBA

Answer

B. ABBA
"aBbA".upper() converts all characters to uppercase, and the output is "ABBA" .

Concepts in Practice 8.1.6 §8.1 source ↗

What is the output of ("a".upper() == "A")?
  1. a.
    True
  2. b.
    False

Answer

A. True
The upper() converts "a" to uppercase ( "A" ). When checking "a".upper() == "A", the output is True.

Concepts in Practice 8.2.1 §8.2 source ↗

Which character is at index 1 in the string "hello"?
  1. a.
    "h"
  2. b.
    "e"
  3. c.
    "o"

Answer

B. "e"
Since indexing starts from 0, the character at index 1 is the second character in the string, which is "e" .

Concepts in Practice 8.2.2 §8.2 source ↗

What is the character at index -2 in the string "Blue"?
  1. a.
    "e"
  2. b.
    "u"
  3. c.
    "l"

Answer

B. "u"
Since negative indexing starts from -1 from the right of the string to the left, "u" is at index -2.

Concepts in Practice 8.2.3 §8.2 source ↗

What is the output of the following code?
word = "chance"
print(word[-1] == word[5])
  1. a.
    True
  2. b.
    False

Answer

A. True
Character "e" in the string "chance" has index of -1 (negative indexing) and 5 (positive indexing).

Concepts in Practice 8.2.4 §8.2 source ↗

What is the output of the following code?
a_string = "Hello world"
print(a_string[2:4])
  1. a.
    "el"
  2. b.
    "ll"
  3. c.
    "llo"

Answer

B. "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 ↗

What is the output of the following code?
location = "classroom"
print(location[-3:-1])
  1. a.
    "ro"
  2. b.
    "oo"
  3. c.
    "oom"

Answer

B. "oo"
The slice [-3:-1] includes characters at index -3 and -2, which are "oo" .

Concepts in Practice 8.2.6 §8.2 source ↗

What is the output of the following code?
greeting = "hi Leila"
name = greeting[3:]
  1. a.
    " Leila"
  2. b.
    "Leila"
  3. c.
    "ila"

Answer

B. "Leila"
The string slice of 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 ↗

What is the correct way of replacing the first character in a string to character "*" in a new string?
  1. a.
    x = "string"
    x[0] = "*"
  2. b.
    x = "string"
    x = "*" + x[1:]
  3. c.
    x = "string"
    x = "*" + x

Answer

B.
x = "string"
x = "*" + x[1:]
To modify string content, a new variable must be created with desired modifications.

Concepts in Practice 8.2.8 §8.2 source ↗

What type of error will result from the following code?
string_variable = "example"
string_variable[-1] = ""
  1. a.
    TypeError
  2. b.
    IndexError
  3. c.
    NameError

Answer

A. TypeError
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.

Concepts in Practice 8.2.9 §8.2 source ↗

What is the output of the following code?
str = "morning"
str = str[1]
print(str)
  1. a.
    TypeError
  2. b.
    m
  3. c.
    o

Answer

C. o
The character at index 1, "o" , is replacing the content of variable str . As such, the print statement prints "o" .

Concepts in Practice 8.3.1 §8.3 source ↗

What is the output of ("a" in "an umbrella")?
  1. a.
    2
  2. b.
    False
  3. c.
    True
  4. d.
    1

Answer

C. True
Since "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 ↗

What is the output of ("ab" in "arbitrary")?
  1. a.
    True
  2. b.
    False

Answer

B. False
"ab" is not a substring of "arbitrary" ; hence, the output is False .

Concepts in Practice 8.3.3 §8.3 source ↗

What is the output of ("" in "string")?
  1. a.
    True
  2. b.
    False

Answer

A. True
Since "" is a substring of length 0 for "string" , the output is True .

Concepts in Practice 8.3.4 §8.3 source ↗

What is the output of the following code?
for c in "string":
  print(c, end = "")
  1. a.
    string
  2. b.
    s
    t
    r
    i
    n
    g
  3. c.
    s t r i n g

Answer

A.
string
The 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 ↗

What is the output of the following code?
count = 0
for c in "abca":
  if c == "a":
    count += 1
print(count)
  1. a.
    0
  2. b.
    1
  3. c.
    2

Answer

C. 2
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 .

Concepts in Practice 8.3.6 §8.3 source ↗

What is the output of the following code?
word = "cab"
for i in word:
  if i == "a":
    print("A", end = "")
  if i == "b":
    print("B", end = "")
  if i == "c":
    print("C", end = "")
  1. a.
    cab
  2. b.
    abc
  3. c.
    CAB
  4. d.
    ABC

Answer

C. CAB
The code iterates over the characters in "cab" and prints each character's uppercase equivalent. Therefore, the output is "CAB" .

Concepts in Practice 8.3.7 §8.3 source ↗

What is the output of (aaa".count("a"))?
  1. a.
    True
  2. b.
    1
  3. c.
    3

Answer

C. 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 ↗

What is the output of ("weather".count("b"))?
  1. a.
    0
  2. b.
    -1
  3. c.
    False

Answer

A. 0
Zero occurrence of "b" exists in "weather" ; hence, the output of "weather".count("b") is 0 .

Concepts in Practice 8.3.9 §8.3 source ↗

What is the output of ("aaa".count("aa"))?
  1. a.
    1
  2. b.
    2
  3. c.
    3

Answer

B. 2
Two substrings "aa" exist in "aaa" , one starting from index 0 and one starting from index 1.

Concepts in Practice 8.3.10 §8.3 source ↗

What is the output of "banana".find("a")?
  1. a.
    1
  2. b.
    3
  3. c.
    5

Answer

A. 1
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.

Concepts in Practice 8.3.11 §8.3 source ↗

What is the output of "banana".find("c")?
  1. a.
    0
  2. b.
    -1
  3. c.
    ValueError

Answer

B. -1
Since "c" is not a substring in "banana" , the find() method returns -1.

Concepts in Practice 8.3.12 §8.3 source ↗

What is the output of "b".find("banana")?
  1. a.
    -1
  2. b.
    0
  3. c.
    ValueError

Answer

A. -1
Since "banana" is not a substring of "b" , -1 is returned.

Concepts in Practice 8.3.13 §8.3 source ↗

What is the output of "school".index("o")?
  1. a.
    3
  2. b.
    4
  3. c.
    -3

Answer

A. 3
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.

Concepts in Practice 8.3.14 §8.3 source ↗

What is the output of "school".index("ooo")?
  1. a.
    3
  2. b.
    4
  3. c.
    ValueError

Answer

C. ValueError
Substring "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 ↗

What is the output of the following code?
sentence = "This is a sentence"
index = sentence.index(" ")
print(sentence[:index])
  1. a.
    "This"
  2. b.
    "This "
  3. c.
    "sentence"

Answer

A. "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 ↗

What is the output of print("Hello {}!".format("Ana"))?
  1. a.
    Ana
  2. b.
    Hello Ana
  3. c.
    Hello Ana!

Answer

C. 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 ↗

What is the output of print("{}:{}".format("One", "1"))?
  1. a.
    One1
  2. b.
    One:1
  3. c.
    1:One

Answer

B. One:1
The two arguments are passed to the template "{}:{}" that outputs "One:1" .

Concepts in Practice 8.4.3 §8.4 source ↗

What is the output of print("{}".format("one", "two", "three"))?
  1. a.
    one
  2. b.
    two
  3. c.
    onetwothree

Answer

A. one
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.

Concepts in Practice 8.4.4 §8.4 source ↗

What is the output of print("Hey {name}!".format(name = "Bengio"))?
  1. a.
    Hey name!
  2. b.
    Hey Bengio
  3. c.
    Hey Bengio!

Answer

C. 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 ↗

What is the output of print("Hey {name}!".format("Bengio"))?
  1. a.
    Hey name!
  2. b.
    KeyError
  3. c.
    Hey Bengio!

Answer

B. KeyError
Since the string template uses a named argument, inputs are only accepted through the corresponding keys. Hence, the code generates KeyError: 'name'.

Concepts in Practice 8.4.6 §8.4 source ↗

What is the output of the following code?
greeting = "Hi"
name = "Jess"
print("{greeting} {name}".format(greeting = greeting, name = name))
  1. a.
    greeting name
  2. b.
    Hi Jess
  3. c.
    Jess Hi

Answer

B. Hi Jess
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" .

Concepts in Practice 8.4.7 §8.4 source ↗

What is the output of the following code?
template = "{name:12}"
formatted_name = template.format(name = "Alice")
print(len(formatted_name))
  1. a.
    5
  2. b.
    12
  3. c.
    "Alice"

Answer

B. 12
The template specifies the minimum length for the name field to be 12, so len(formatted_name) is 12 .

Concepts in Practice 8.4.8 §8.4 source ↗

What is the output of the following code?
template = "{greeting:>6}"
formatted_greeting = template.format(greeting = "Hello")
print(formatted_greeting[0])
  1. a.
    H
  2. b.
    " Hello"
  3. c.
    Space character

Answer

C. Space character
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.

Concepts in Practice 8.4.9 §8.4 source ↗

What is the output of the following code?
template = "{:5}"
print(template.format("123456789"))
  1. a.
    56789
  2. b.
    123456
  3. c.
    123456789

Answer

C. 123456789
The field length specification enforces the minimum length of the string. Inputs that are longer than the specified minimum length will not be modified.

Concepts in Practice 8.4.10 §8.4 source ↗

What is the output of print('{0:.3f}'.format(3.141592))?
  1. a.
    3.141592
  2. b.
    3.1
  3. c.
    3.142

Answer

C. 3.142
The .3 format specification shows that the output precision must be three decimal places.

Concepts in Practice 8.4.11 §8.4 source ↗

What is the output of print('{:1>3d}'.format(3))?
  1. a.
    113
  2. b.
    311
  3. c.
    3.000

Answer

A. 113
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.

Concepts in Practice 8.4.12 §8.4 source ↗

What is the output of print('{:+d}'.format(123))?
  1. a.
    123
  2. b.
    +123
  3. c.
    :+123

Answer

B. +123
{:+d} formats the decimal number by adding the positive sign.

Concepts in Practice 8.5.1 §8.5 source ↗

What is the output of print("1*2*3*".split('*'))?
  1. a.
    ["1", "*", "2", "*", "3", "*"]
  2. b.
    ["1", "2', "3"]
  3. c.
    [1, 2, 3]

Answer

B. ["1", "2', "3"]
When "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 ↗

What is the output of print("a year includes 12 months".split())?
  1. a.
    ["a year includes 12 months"]
  2. b.
    ["a", "year", "includes", 12, "months"]
  3. c.
    ["a", "year", "includes", "12", "months"]

Answer

C. ["a", "year", "includes", "12", "months"]
When "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 ↗

What is the output of the following code?
s = """This
is a
test"""

out = s.split()
print(out)
  1. a.
    Error
  2. b.
    ['This', 'is', 'a', 'test']
  3. c.
    >['This', 'is a', 'test']

Answer

B. ['This', 'is', 'a', 'test']
When splitting the multi-line string, the output is ['This', 'is', 'a', 'test'].

Concepts in Practice 8.5.4 §8.5 source ↗

What is the output of the following code?
elements = ['A', 'beautiful', 'day', 'for', 'learning']

print(",".join(elements))
  1. a.
    'A beautiful day for learning'
  2. b.
    ['A, beautiful, day, for, learning']
  3. c.
    'A,beautiful,day,for,learning'

Answer

C. 'A,beautiful,day,for,learning'
Elements of the list 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 ↗

What is the length of the string "sss".join(["1","2"])?
  1. a.
    2
  2. b.
    5
  3. c.
    8

Answer

B. 5
The output of "sss".join(["1","2"]) is "1sss2" , which has a length of 5.

Concepts in Practice 8.5.6 §8.5 source ↗

What is the value stored in the variable out?
s = ["1", "2"]
out = "".join(s)
  1. a.
    12
  2. b.
    "12"
  3. c.
    "1 2"

Answer

B. "12"
Since the separator is an empty string, elements of the list will be concatenated without a separator and the output will be "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.