Unit 2. Developing Programming Tools
Revision Date: Jun 27, 2015 (Version 1.2)Summary
Students will use the online book Python for Informatics to complete a two-session guided lab in which they will explore the use of strings in Python.
Outcomes
Students will be able to:
len
function to get the number of characters in a string[m:n]
find
method and slicingOverview
Session 1:
Session 2:
for
and while
loops. Students must decide which loop structure is appropriate, based on the nature of a program's requirements.Student computer usage for this lesson is: required
In the Lesson Resources folder:
Other:
find
method for Python 3.4 can be found at:Journal Question: What is a string?
stringtester.py
to test code as they go though the lesson.print
function, as the book was written prior to Python 3.x.Section 6.1: A string is a sequence
Section 6.2: Getting the length of a string using len
len
function, which returns the number of characters in a string. Explain that empty spaces between words and punctuation also count as characters. Remind students of previous lessons concerning ASCII values for all characters, including blank spaces and punctuation.fruit
variable from the previous sample, have students return the number of characters using the len
function.IndexErrors:
that the length of the string and the highest index value in that same string are not the same value. The length of the string banana
is 6; the index values are numbered from 0 to 5, inclusive.len
, then have students type the sentence into the console in PyCharm to check their answers.Section 6.3: Traversal through a string with a loop
while loop and incrementing the index value of each character.
Journal Question: Explain why the length of a string is one digit higher than the highest index value of the same string.
word = "alphabet"
index = 0while index < len(word):
letter = word[index]
print (letter)
index = index + 1
index += 1
in place of index = index + 1
.stringtester.py
file used to test the code that they created during the last lesson. Section 6.3: Traversal through a string with a loop continued
Sample solution:
fruit = "watermelon"
length = len(fruit)
index = length - 1
while index >= 0:
letter = fruit[index]
print(letter)
index -= 1
for
loop instead of a while
loop to traverse a string.for
loop to traverse a string.while
loop than a for
loop to traverse a string and share their ideas. Expect answers such as “it is easier to make a for
loop” and “it is harder to move backwards through a for
loop than a while
loop.”char
in the example is used as a variable and is not a keyword in Python. It can be replaced with another word or letter. Have students use x
instead of char
in their sample for
loop code to test this concept. This is an opportunity to have a discussion about using meaningful variable names so they have self-documenting code. Section 6.4: String slices
[n:m]
syntax to indicate returning a string from the nth character to the mth character, not including the mth character.print(fruit[0:3]) # returns app because a is in the 0th position and the second p is in the 2nd position.
# The slice goes to ‘p’ not including the ‘p’.
print(fruit[:3]) # Still returns app because a is in the 0th position.
# Leaving the first index blank begins the slice at the beginning of the string,
# and the second p is in the 2nd position.
# The slice goes to ‘p’ not including the ‘p’.
print(fruit[3:]) # Returns le because l is in the 3rd position.
# Leaving the second index blank ends the slice at the end of the string.
print(fruit[:]) # Returns apple because n begins at zero and m goes to the end of the string.
fruit
is a string, what does fruit[:]
mean?Section 6.10: Parsing strings
find
method and apply slicing in this section.find
method. Explain that the find
method returns the position at which the substring we are searching for begins.find
method without error.message = 'Meet me at the clock tower @ 7:00 a.m.'
atSign = message.find('@')
print (atSign)
This code sample returns 27
, indicating that the @ sign is at index value 27 in the string.
Students can work in pairs while new concepts are introduced and practiced.
One advanced student could be assigned to be the "checker" for each row and have them raise a flag or something similar when they have checked off everybody in their row as having one small group of programming exercises complete and correct. Possibly offer a token prize to the winning row.
For example, after Section 6.3, the following exercise could be assigned:
Code checks for understanding and concept checks for understanding are provided with each new function, method, or concept introduced.
Summative coding assessment:
Sample answer code:
name = 'Pat Miller'
space = name.find(' ')
firstName = name[0:space]
print (firstName)
It is recommend that a 10-question multiple choice quiz that requires students to evaluate code samples from these lessons, determining the output or possible outcomes when the code is run be developed.