Prévia do material em texto
CHECKPOINT
String indexing
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-2-string-slicing)
CONCEPTS IN PRACTICE
Accessing characters in a string using indexing
1. Which character is at index 1 in the string "hello"?
a. "h"
b. "e"
c. "o"
2. What is the character at index -2 in the string "Blue"?
a. "e"
b. "u"
c. "l"
3. What is the output of the following code?
word = "chance"
print(word[-1] == word[5])
a. True
b. False
String slicing
String slicing is used when a programmer must get access to a sequence of characters. Here, a string slicing
operator can be used. When [a:b] is used with the name of a string variable, a sequence of characters
starting from index a (inclusive) up to index b (exclusive) is returned. Both a and b are optional. If a or b are not
provided, the default values are 0 and len(string), respectively.
EXAMPLE 8.2
Getting the minutes
Consider a time value is given as "hh:mm" with "hh" representing the hour and "mm" representing the
minutes. To retrieve only the string's minutes portion, the following code can be used:
time_string = "13:46"
minutes = time_string[3:5]
print(minutes)
8.2 • String slicing 201
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
The above code's output is:
46
EXAMPLE 8.3
Getting the hour
Consider a time value is given as "hh:mm" with "hh" representing the hour and "mm" representing the
minutes. To retrieve only the string's hour portion, the following code can be used:
time_string = "14:50"
hour = time_string[:2]
print(hour)
The above code's output is:
14
CONCEPTS IN PRACTICE
Getting a substring using string slicing
4. What is the output of the following code?
a_string = "Hello world"
print(a_string[2:4])
a. "el"
b. "ll"
c. "llo"
5. What is the output of the following code?
location = "classroom"
print(location[-3:-1])
a. "ro"
b. "oo"
c. "oom"
6. What is the output of the following code?
202 8 • Strings
Access for free at openstax.org
greeting = "hi Leila"
name = greeting[3:]
a. " Leila"
b. "Leila"
c. "ila"
String immutability
String objects are immutable meaning that string objects cannot be modified or changed once created. Once
a string object is created, the string's contents cannot be altered by directly modifying individual characters or
elements within the string. Instead, to make changes to a string, a new string object with the desired changes
is created, leaving the original string unchanged.
CHECKPOINT
Strings are immutable
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-2-string-slicing)
CONCEPTS IN PRACTICE
Modifying string content
7. What is the correct way of replacing the first character in a string to character "*" in a new string?
a. x = "string"
x[0] = "*"
b. x = "string"
x = "*" + x[1:]
c. x = "string"
x = "*" + x
8. What type of error will result from the following code?
string_variable = "example"
string_variable[-1] = ""
a. TypeError
b. IndexError
c. NameError
9. What is the output of the following code?
str = "morning"
str = str[1]
print(str)
8.2 • String slicing 203
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
a. TypeError
b. m
c. o
TRY IT
Changing the greeting message
Given the string "Hello my fellow classmates" containing a greeting message, print the first word by
getting the beginning of the string up to (and including) the 5th character. Change the first word in the
string to "Hi" instead of "hello" and print the greeting message again.
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-2-string-slicing)
TRY IT
Editing the string at specified locations
Given a string variable, string_variable, and a list of indexes, remove characters at the specified indexes
and print the resulting string.
Input:
string_variable = "great"
indices = [0, 1]
prints eat
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-2-string-slicing)
8.3 Searching/testing strings
Learning objectives
By the end of this section you should be able to
• Use the in operator to identify whether a given string contains a substring.
• Call the count() method to count the number of substrings in a given string.
• Search a string to find a substring using the find() method.
• Use the index() method to find the index of the first occurrence of a substring in a given string.
• Write a for loop on strings using in operator.
204 8 • Strings
Access for free at openstax.org
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
https://openstax.org/books/introduction-python-programming/pages/8-2-string-slicing
in operator
The in Boolean operator can be used to check if a string contains another string. in returns True if the first
string exists in the second string, False otherwise.
CHECKPOINT
What is in the phrase?
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-3-searchingtesting-strings)
CONCEPTS IN PRACTICE
Using in operator to find substrings
1. What is the output of ("a" in "an umbrella")?
a. 2
b. False
c. True
d. 1
2. What is the output of ("ab" in "arbitrary")?
a. True
b. False
3. What is the output of ("" in "string")?
a. True
b. False
For loop using in operator
The in operator can be used to iterate over characters in a string using a for loop. In each for loop iteration,
one character is read and will be the loop variable for that iteration.
CHECKPOINT
for loop using in operator
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-3-searchingtesting-strings)
CONCEPTS IN PRACTICE
Using in operator within for loop
4. What is the output of the following code?
8.3 • Searching/testing strings 205
https://openstax.org/books/introduction-python-programming/pages/8-3-searchingtesting-strings
https://openstax.org/books/introduction-python-programming/pages/8-3-searchingtesting-strings
https://openstax.org/books/introduction-python-programming/pages/8-3-searchingtesting-strings
https://openstax.org/books/introduction-python-programming/pages/8-3-searchingtesting-strings
Chapter 8 Strings
8.3 Searching/testing strings