Strings

Text in Python

Grades 8+
  • Web
Request Quote
for Middle School Curriculum

Lesson Plan

Lesson: Strings

Time: 40+ mins

Introduction

In this lesson, students will apply coding concepts to handle text and manipulate strings using Python.

Vocabulary

  • String: A string is a sequence of characters (letters) enclosed in either double or single quotes. Multi-line strings can be written between three single quotes ('''). Strings can be stored as variables and perform different functions on the strings. Individual pieces of a string can be accessed using an index, where the first character is stored in index 0. In Python, strings are immutable.
  • Immutable: Immutable objects are objects that cannot be modified after they are created. Strings are immutable and while strings cannot be manipulated, new strings can be formed with the changes applied. For example, if two strings are concatenated together, a third string is created that is the length of the first two strings combined. Here is an example:
    hello = “Hello,”
    world = “World!”
    print(hello + world)
  • Index: A position in a string or other object in Python. Indexes in Python start at 0 and increase by 1 as you move to the right. Python will produce an error if you try to access an invalid index, an index that is not in the string or object. In Python, you can also use negative indexes to reference the index from the end of the string. For example, the index -1 is the last character in a string.
  • Substring: A sub-sequence of characters in a string. "hello" is a substring of "hellogoodbye".
  • "in" Operator: The "in" operator is a binary operator that will return True if the string on the left is in the string on the right. Here is an example:
  • "not in" Operator: The "not in" operator is a binary operator that will return True if the string on the left is not in the string on the right. Here is an example:
     

Objectives

Students will...
  • Define a string
  • Apply specific functions to strings to change the strings in certain ways
  • Access specific parts of strings using indexes
  • Check if a specific substring is in a larger string
  • Find and replace a specific substring
  • Format strings

Materials

  • Computers (1 per student) with student account access to Tynker.com

Warm-Up (5 minutes)

Ask students to answer the following true/false questions:
  • True or false: A boolean is a value that is either True or False. (True)
  • True or false: In Python, it’s impossible to reassign values to variables. (False)
  • True or false: The following are examples of conditional statements: “if,” “elif,” and “else.” (True)
  • True or false: It is impossible to create graphics or shapes using Python. (False)
  • True or false: One of the purposes of loops is to reduce the number of lines of code. (True)

Activities (35 minutes)

Facilitate as students complete the Strings modules on their own:
1. Strings (Tutorial)
    • Students will read a short document that explains a string, which is a sequence of characters enclosed in quotation marks.
    • Optional: Ask students, “True or false: A string can only be enclosed in double quotes.” (False. A string can be enclosed in single quotes (‘), double quotes (“), or triple single quotes (‘ ‘ ‘).)
    • Encourage students to slightly modify the given code in the first example. Here’s a suggestion: Ask students to add new characters and numbers inside the string. How did their output change?
    • Is the animated immutability description moving too quickly? Click the pause button to stop the animation, and click it again to continue playing.
    • Are students struggling with the “Digit length” puzzle? Tell them to use the code in the “string length” section as a reference and to include the following as part of their solution: (str(n))
    • Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
2. String Indexes (Tutorial)
    • In this module, students will read a short document that explains string indexes.
    • Optional: Before students select the “play” button in the first two examples, ask them to write down what they think the code will output.
    • Are students struggling with the “Digits of pi” puzzle? Give a hint: Tell them to use the “accessing characters” code as a reference. Also, tell students that they are printing numbers after the decimal place.
    • If students are struggling with the “Punctuation check” puzzle, direct their attention to the provided hint or encourage them to brainstorm possible solutions with a neighbor.
3. Index Ranges (Tutorial)
    • Students will read a short document that explains index ranges.
    • Encourage students to slightly modify the index range in the first two examples. Was the output what they expected it to be?
    • Are students struggling with the “Print hundred millionaire” puzzle? Give a hint: Tell them that they need to convert the net worth into a string and part of their solution might look something like this:
      net_worth = str(net_worth)
    • Are students struggling with the “Changing area codes” puzzle? Give a hint: Tell them to try including a no start index range and a no end index range as part of their solution.
    • Tell students to click the “Next” button to move on to the next module.
4. String Functions (Tutorial)
    • Students will read a short document that explains string functions.
    • Optional: Before students select the “play” button in the examples, ask them to write down what they think the code will output.
    • Are student struggling with the “Capitalize first half” puzzle? Tell them to try using the “len()” function to find the index where the word should be split, and part of their solution might look something like this:
      middle = len(word) / 2
    • Tell students to click the “Next” button to move on to the next module.
5. Counting Instances (Tutorial)
    • Students will read a short document that explains counting instances.
    • Optional: Ask students, “What does the ‘count()’ function do?” (Returns the number of times a given sequence of characters appears in a string.)
    • In the “nucleotides” section, check that students are experimenting with their code and printing the number of guanine, cytosine, and thymine nucleotides.
    • If students are struggling with the “GC content” puzzle, direct their attention to the provided hint or encourage them to brainstorm possible solutions with a neighbor.
    • Are students struggling with the “AT/GC” puzzle? Give a hint: Tell students to format their output as a float. Part of their solution might look something like this:
      print(float(at) / gc)
6. String Operators (Tutorial)
    • Students will read a short document that introduces the “in” operator.
    • Optional: Encourage students to slightly modify the code in the examples and create a similar program. Was their output what they expected it to be?
    • Are students struggling with the “Hair color” puzzle? Give a hint: Tell students to try using the following operators: in, and, or.
7. Finding the Index (Tutorial)
    • In this module, students will read a short document that explains finding the index.
    • Optional: Ask students to fix the error in the first section. Here’s one possible solution:
      dna = "AAACGTAAAAACGT"
      print( dna.index("AC") )
    • Are students struggling with the “A before Z” puzzle? Give a hint: Tell students to try including the less than operator (<) as part of their solution.
    • Are students struggling with the “Necklace replacement” puzzle? Give a hint: Tell students that they will need to use an intermediate letter to store where the black beads were. Part of their solution might look something like this:
      necklace = necklace.replace(“b”, “X”)
    • Are students struggling with the “Necklace replacement repeated” puzzle? Give a hint: Tell students to use their solution from the previous puzzle, but add a “while” loop to it. Explain that the purpose of the “while” loop is to repeat the replacement process.
8. String Formatting (Tutorial)
    • Students will read a short document that explains string formatting.
    • This module does not contain puzzles.
    • Optional: Encourage students to use the code in the provided examples as a reference as they create their own string formatting examples.
9. Quiz
    • This quiz requires students to apply concepts from this lesson to solve 2 different puzzles. There are no multiple choice questions.

Discussion Questions/Follow-Up Activities (20 minutes)

More true/false questions: Strings
    • True or false: Strings can only contain letters. (False)
    • True or false: Strings are immutable, so they cannot be modified after they are created (i.e., you cannot change the character at a certain index by assigning a new value to it). (True)
    • True or false: Indexes start at 0 and increase by one for every subsequent character. (True)
    • True or false: A string can only be enclosed with double quotes (“). (False. A string can be enclosed in single quotes (‘), double quotes (“), or triple single quotes (‘ ‘ ‘))
Bonus: Ask students to create their own true/false questions about concepts covered in this lesson and quiz a neighbor.

US Standards

  • CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
  • CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
  • CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-15, 2-AP-17, 3A-AP-17, 3B-AP-11, 3B-AP-12
  • CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
  • ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
Strings
Python 201
Lesson Includes
  • 9 Activities
  • 1 Completion Badge
Languages
Python
Experience
Advanced