Unit 2. Developing Programming ToolsRevision Date: Jul 27, 2017 (Version 2.1)
Students will use the online book Python for Everybody to complete a two-session guided lab in which they will explore the use of strings in Python.
Students will be able to:
lenfunction to get the number of characters in a string
findmethod and slicing
whileloops. 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:
findmethod for Python 3.4 can be found at:
Journal Question: What is a string?
stringtester.pyto test code as they go though the lesson.
Section 6.1: A string is a sequence
Section 6.2: Getting the length of a string using len
lenfunction, 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.
fruitvariable from the previous sample, have students return the number of characters using the
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
bananais 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
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 = 0
while index < len(word):letter = word[index]
index = index + 1
index += 1in place of
index = index + 1.
stringtester.pyfile used to test the code that they created during the last lesson.
Section 6.3: Traversal through a string with a loop continued
fruit = "watermelon" length = len(fruit) index = length - 1
while index >= 0: letter = fruit[index] print(letter) index -= 1
forloop instead of a
whileloop to traverse a string.
forloop to traverse a string.
whileloop than a
forloop to traverse a string and share their ideas. Expect answers such as “it is easier to make a
forloop” and “it is harder to move backwards through a
forloop than a
charin 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
charin their sample
forloop 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.
fruitis a string, what does
Section 6.10: Parsing strings
findmethod and apply slicing in this section.
findmethod. Explain that the
findmethod returns the position at which the substring we are searching for begins.
findmethod without error.
message = 'Meet me at the clock tower @ 7:00 a.m.'
atSign = message.find('@')
This code sample returns
27, indicating that the @ sign is at index value 27 in the string.
Journal: How are string functions used in everyday life? (search engines to find information quickly, school databases to look up student information, many more). How much more efficient is a computer at looking through millions of strings of data compared to a human? Extension: what if you were looking for a particular face in a crowd in a video, how could computational tools enhance that process?
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]
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.