7.17. Group Work - Strings¶
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.
Note
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Many interesting problems involve manipulating sequences of data. You’ve learned about strings before, but this activity provides a more in-depth look at what they can do.
Content Learning Objectives
After completing this activity, students should be able to:
Explain the syntax and meaning of slice operations, with and without indexes.
Name four methods that strings provide, and describe what each method does.
Process Skill Goals:
During the activity, students should make progress toward:
Gaining insight about data structures from many examples (Information Processing).
7.17.1. Indexing and Slicing¶
A string is a sequence of characters in single quotes(’) or double
quotes (“). Depending on the application, we can treat a string as
a single value (e.g., sentence
), or we can access individual
characters using square brackets (e.g., sentence[0]
). We can also
use slice notation (e.g., sentece[4:8]
) to refer to a range of
characters. In fact, all types of sequences (including list
and tuple
) support indexing and slicing.
Run this code to see what it prints. Its output will help you answer the questions below.
- 0, 9
- Correct! Indices begin at 0 and increment by 1 for each following character.
- 0, 10
- Incorrect! There are 10 characters in the string, and the first character is index 0, so what is the index of the last character? Try again.
- 1, 9
- Incorrect! Indices begin at 0. Try again.
- 1, 10
- Incorrect! Indices begin at 0. Try again.
11-9-2: What is the positive index of the first character in the sentence
string? What about the last?
- -9, 0
- Incorrect! 0 is the index of the first character in the string, not the last. Try again.
- -9, -1
- Incorrect! There are 10 characters in the string, so if the last one has an index of -1, what is the negative index of the first? Try again.
- -10, 0
- Incorrect! 0 is the index of the first character in the string, not the last. Try again.
- -10, -1
- Correct! Negative indices start at the length of the string times -1. Then, they increment by 1 for each following character.
11-9-3: What is the negative index of the first character in the sentence
string? What about the last?
- a, r
- Correct! 'a' is the third character in the string (so its index is 2) and 'r' is the second-to-last character in the string.
- a, a
- Incorrect! "sentence[-2]" is the second-to-last character in the string. Try again.
- _, r
- Incorrect! "sentence[2]" is the third character in the string. Try again.
- _, a
- Incorrect! "sentence[2]" is the third character in the string and "sentence[-2]" is the second-to-last character in the string. Try again.
11-9-4: What is sentence[2]
? What about sentence[-2]
?
Let’s take a look at how the :
operator works for slicing
a string. Consider the example sentence[m:n]
. The value at
m
is the first character in the slice. It is the same value
as sentence[m]
. However, the value at n
is not the same
value as sentence[n]
. n
is the index after the last
character included in the slice.
You can also reference only a single number when creating a
slice. The slice [m:]
means “from the index m
to the
end.” The slice [:n]
means “from the beginning to the index
just before n
” (i.e., the first n
characters).
7.17.2. Common String Methods¶
Strings have methods (built-in functions) that can be called using dot notation. See https://docs.python.org/3/library/stdtypes.html#string-methods for a list of Python string methods.
Run this code to see what it prints. Its output will help you answer the questions below.
- True
- Incorrect! Take a closer look at the code above. Try again.
- False
- Correct! When line 3 of the code above prints(dna), it is still capitalized, even though in the previous line, "lower" was called on dna.
11-9-8: True or false: the lower
method changes the contents of the string it is called on.
- Finds and replaces the first instance of a substring in a larger string with a different string, changing the original string.
- Incorrect! Strings are "immutable", which means that their value is unchanged by methods. Try again.
- Finds and replaces the first instance of a substring in a larger string with a different string, returning a changed version of the string but without changing the original string.
- Incorrect! replace() replaces all occurrences of the substring, not just the first one. Try again.
- Finds and replaces every instance of a substring in a larger string with a different string, changing the original string.
- Incorrect! Strings are "immutable", which means that their value is unchanged by methods. Try again.
- Finds and replaces every instance of a substring in a larger string with a different string, returning a changed version of the string but without changing the original string.
- Correct! replace() replaces all occurrences of the substring, not just the first one. However, it doesn't change the original string.
11-9-9: What does the replace
method do? Assume it is called with two arguments.
You may have noticed that it isn’t possible to call the
replace
method on dna
, but calling it on dna[0]
is okay. This is because the “list” data type does not
include a replace method. However, strings allow you to
“find and replace” any text. Keep in mind, however, that
string variables don’t change after applying a method.
For this reason, strings are referred to as immutable
(i.e., the value never changes).
- name.capitalize()
- Incorrect! Because strings are immutable, the value of "name" would remain unchanged. Try again.
- name = name.capitalize()
- Correct! Because strings are immutable, the value of "name" must be changed to equal the string returned by "name.capitalize()".
- name = name.capitalize(name)
- Incorrect! The "capitalize" method has no parameters, just like the "lower" method. Try again.
- capitalize(name)
- Incorrect! "capitalize" is a method, so it must be called using dot notation. Try again.
11-9-10: The capitalize
method capitalizes the first character of a string. If I wanted to capitalize the first letter of the string name = "robby"
, and change the value of name
, what line of code would I write?
- nofirst = name[1:].capitalize()
- Correct! This creates the slice "obby" and then capitalizes the first letter of it.
- nofirst = name[1:4].capitalize()
- Incorrect! This would make "nofirst" = "Obb", not "Obby". Try again.
- nofirst = name.capitalize()[1:]
- Incorrect! This would make "nofirst" = "obby" because "capitalize" was called before the slice. Try again.
- nofirst = name[1:].capitalize
- Incorrect! This would cause a SyntaxError, as "capitalize" needs to be called with parentheses. Try again.
11-9-11: If I wanted to create a new string, nofirst
, whose value equals “Obby”, what line of code would I write? Reminder: name = "robby"
.
- nofirst = replace(nofirst[-2:0], nofirst[0:2])
- Incorrect! "replace" is a string method, so it must be called with dot notation. Try again.
- nofirst = nofirst.replace(nofirst[2:], nofirst[-2:])
- Incorrect! This wouldn't change the string at all because it would replace the last two characters with themselves. Try again.
- nofirst = nofirst.replace(nofirst[:2], nofirst[-2:])
- Incorrect! This would replace "Ob" with "by, making "nofirst" = "byby". Try again.
- nofirst = nofirst.replace(nofirst[-2:], nofirst[:2])
- Correct! This replaces all instances of "by" in "nofirst" with "Ob".
11-9-12: The string nofirst
now equals “Obby”. What line of code would I write if I wanted to change its value to “ObOb”?
-
11-9-13: Match each string method to a description of what it does.
Keep trying! Run the code block above if you need more help.
- split
- Returns a list of substrings which were separated by a specific character/string.
- lower
- Returns a new string with all letters changed to lowercase.
- replace
- Returns a new string with all occurences of a specific substring substituted with another string.
- capitalize
- Returns a new string with the first letter changed to uppercase.
The code blocks below have been mixed up! Rearrange them so that the program prints “Georgington”. Watch out - there are three code blocks that are unused in the solution!
There are dozens of other string methods not shown in this section of the ebook. Read Python’s online documentation at https://docs.python.org/3/library/stdtypes.html#string-methods to learn about more! They can be very helpful.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.