Skip to main content

Section 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 1 .

Note 5.21.1.

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:
  • Recognize strings.
  • Use string indices.
  • Understand how slice works for both positive and negative indices.
  • Use input and convert between strings and numbers.
  • Recognize common string methods.
Process Objectives:
  • Predict the output from code.
  • Modify code to produce the correct output.
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.

Subsection 5.21.1 String Indices

Checkpoint 5.21.2.

Q-1: What is the last thing that will be printed when the code below runs?

Checkpoint 5.21.3.

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

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.

Checkpoint 5.21.5.

Q-3: What type of thing is “Hello”, i.e. what class does it belong to?

Checkpoint 5.21.6.

Q-4: What type of thing is 42, i.e. what class does it belong to?

Checkpoint 5.21.7.

Q-5: What is the symbol that is used to append (concatenate) strings together?

Subsection 5.21.2 String Slices

Checkpoint 5.21.8.

Q-6: What is the last thing that will be printed when the code below runs?

Checkpoint 5.21.9.

Run the code below to see what it prints.

Note 5.21.10.

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

Checkpoint 5.21.11.

Q-8: Use the slice operator to return the first three characters from the variable dna?

Checkpoint 5.21.12.

Q-9: Use the slice operator to return a new string with just the last three characters from the variable named dna using a negative index?

Checkpoint 5.21.13.

Q-10: What index will the slice (substring) start with if the start index isn't specified?

Checkpoint 5.21.14.

    Q-11: What index is the default end index if it isn't specified in a slice?
  • 0
  • This is the default start index
  • -1 (the index of the last character)
  • Since a slice does not include the character at the specified end index this would stop at the second to last character in the string
  • the length of the string
  • The slice returns a string with all the characters from the start to the end minus one. That is the same as the length of the string minus one.

Checkpoint 5.21.15.

Q-12: What built-in function tells you the number of characters in a string?

Checkpoint 5.21.16.

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

Subsection 5.21.3 Input and Converting Between Strings and Numbers

Checkpoint 5.21.17.

Run the code below to see what it prints.

Checkpoint 5.21.18.

Q-15: What is the name of the built-in function that will convert a string to an integer?

Checkpoint 5.21.19.

Q-16: What is the name of the built-in function that will convert an integer to a string?

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

Checkpoint 5.21.20.

Q-17: What will be returned from the get_user_name function below?

Checkpoint 5.21.21.

Run the code below to see what it prints.

Checkpoint 5.21.22.

Q-19: What does the function find return if the character is not found in the string?

Note 5.21.23.

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

Checkpoint 5.21.24.

Checkpoint 5.21.25.

Run the code below to see what it prints.

Checkpoint 5.21.26.

For more information on string methods see https://www.w3schools.com/python/python_ref_string.asp 2 .
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.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=func_string_groupsub data-size_limit=4> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>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.</p> </div> </div>
https://cspogil.org/Home
https://www.w3schools.com/python/python_ref_string.asp