# 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.

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.

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).

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.

You have attempted of activities on this page