5.21. Group Work: Functions and 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.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

A string is a sequence of characters enclosed in quotes. In Python you can use pairs of single or double quotes to enclose a string. You can even use tripe quotes when a string covers more than one line.

5.21.1. String Indices

Run the code below to see what it prints. Then fix it to pass the given test. It should return a string with the first character of the first name and first character of the last name.

Note

Use [index] to get a character from a string. The first character in a string is at index 0 and the last is at the length of the string minus 1 (also know as index -1 in Python). Also note that you can use pairs of single quotes or double quotes to create a string. This is useful when the string includes a single quote.

Fix the function get_initials above to return a string with the first letter of the first name followed by the first letter of the last name.

5.21.2. String Slices

Run the code below to see what it prints.

Note

Use the slice [start:end] operator to get a slice (substring) from a string. It will return a new string starting at the start and including all the characters up to just before the end (end - 1).

Write a function join_no_first that takes two strings a and b and returns a new string with all the characters in string a except the first one followed by all the characters in b except the first one. For example, join_no_first(‘hi’, ‘bye’) would return ‘iye’.

5.21.3. Input and Converting Between Strings and Numbers

Run the code below to see what it prints.

5.21.4. String Methods

Strings have methods (functions) that operate on a string object using dot-notation as shown in the example code below.

Run the code below to see what it prints.

Note

Strings are immutable, which means that they do not change. String methods that appear to change a string return a new string.

Run the code below to see what it prints.

For more information on string methods see https://www.w3schools.com/python/python_ref_string.asp.

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