Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

8.1 String operations

1.
a. Since a's ASCII value is less than b's ASCII value, the output for "aaa" < "aab" is True.
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.
4.
a. "aBbA".lower() converts all characters to lowercase, and the output is "abba" .
5.
b. "aBbA".upper() converts all characters to uppercase, and the output is "ABBA" .
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).
4.
b. a_string[2:4] is a slice including the third and fourth characters, which is "ll" .
5.
b. The slice [-3:-1] includes characters at index -3 and -2, which are "oo" .
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" .
7.
b. To modify string content, a new variable must be created with desired modifications.
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

1.
c. Since "a" is a substring of "an umbrella" , the output of "a" in "an umbrella" is True .
2.
b. "ab" is not a substring of "arbitrary" ; hence, the output is False .
3.
a. Since "" is a substring of length 0 for "string" , the output is True .
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" .
7.
c. "aaa".count("a") returns 3 , which is the number of occurrences of "a" in string "aaa" .
8.
a. Zero occurrence of "b" exists in "weather" ; hence, the output of "weather".count("b") is 0 .
9.
b. Two substrings "aa" exist in "aaa" , one starting from index 0 and one starting from index 1.
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.
11.
b. Since "c" is not a substring in "banana" , the find() method returns -1.
12.
a. Since "banana" is not a substring of "b" , -1 is returned.
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!" .
2.
b. The two arguments are passed to the template "{}:{}" that outputs "One:1" .
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.
10.
c. The .3 format specification shows that the output precision must be three decimal places.
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.
12.
b. {:+d} formats the decimal number by adding the positive sign.

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.
3.
b. When splitting the multi-line string, the output is ['This', 'is', 'a', 'test'].
4.
c. Elements of the list elements are concatenated together using a comma, so the output is 'A,beautiful,day,for,learning' .
5.
b. The output of "sss".join(["1","2"]) is "1sss2" , which has a length of 5.
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" .
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.