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 .
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:
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.
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.
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.
Checkpoint 5.21.6.
Checkpoint 5.21.7.
Subsection 5.21.2 String Slices
Checkpoint 5.21.8.
Checkpoint 5.21.9.
Run the code below to see what it prints.
Checkpoint 5.21.11.
Checkpoint 5.21.12.
Checkpoint 5.21.13.
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.
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.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.
Checkpoint 5.21.21.
Run the code below to see what it prints.
Checkpoint 5.21.22.
Checkpoint 5.21.24.
Checkpoint 5.21.25.
Run the code below to see what it prints.
Checkpoint 5.21.26.
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://www.w3schools.com/python/python_ref_string.asp