Professional Documents
Culture Documents
McMullen ProgwPython 1e Mod05 PowerPoint
McMullen ProgwPython 1e Mod05 PowerPoint
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved.
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (1 of 3)
• 5.1 Characters
• 5.1.1 List the variety of data that is classified as a character.
• 5.1.2 Initialize data as a character.
• 5.1.3 Describe the punctuation conventions used for character data.
• 5.1.4 State the storage required for ASCII character data.
• 5.1.5 Explain the difference between numbers and digits.
• 5.1.6 Format character output separated by spaces or on separate lines.
• 5.1.7 List some common functions or methods available for manipulating character data.
• 5.2 String Data Type
• 5.2.1 Explain that characters combine to form strings.
• 5.2.2 State that Python characters are strings of length 1.
• 5.2.3 Describe the punctuation conventions used for string literals.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (2 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (3 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (1 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (2 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (3 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (4 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (5 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (6 of 7)
Figure 5-8
Changing the case
of a character
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Characters (7 of 7)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 String Data Type (1 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 String Data Type (2 of 3)
• Escape characters
• An escape sequence embeds a command within a string and begins with a backslash
• Escape sequences allow punctuation such as double quotation marks to appear within
text strings without causing errors
• Include \" (quotation mark), \\ (backslash), \n (newline), and \t (tab)
• String indexes
• Index: a number that indicates a character's position in a string
• The first character in the string is referenced by index 0, the next character is index 1, et
cetera
• Sample code for returning the value at a given index of a string (output is C):
company_name = "Campbell's"
letter = company_name[0]
print(letter)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 String Data Type (3 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.1: Knowledge Check
2. True or False: If the variable my_three is assigned the value of "3", the method
my_three.isdigit() returns the Boolean True.
3. To include a double quotation mark, backslash, or blank line within a string, you use a(n) _____.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.1: Knowledge Check Answers
2. True or False: If the variable my_three is assigned the value of "3", the method
my_three.isdigit() returns the Boolean True.
Answer: True
3. To include a double quotation mark, backslash, or blank line within a string, you use a(n) _____.
Answer: 4
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 String Functions (1 of 5)
• String manipulation
• Common ways to manipulate strings:
• Find the length of a string
• Change the case of a string
• Check if a string contains a specific character
• Retrieve a substring from a longer string
• String length
• Strength length is useful for reversing strings, ensuring they are not too long, et cetera
• Python's len() function returns a string's length as an integer
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 String Functions (2 of 5)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 String Functions (3 of 5)
Method Description Code Output
count() Returns the number of times a "banana".count("a") 3
specified value occurs in a
string.
endswith() Returns True if the string ends "alphabet".endswith("bet") True
with the specified value.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 String Functions (4 of 5)
• Change case
• Case can be significant when searching and sorting data
• Case sensitivity: the concept that uppercase letters are different from lowercase letters
• Result of the different ASCII codes for uppercase and lowercase letters
• The lower() method can be called on two strings prior to comparing them
• Find the location of a character
• The find() method produces the index position of the first occurrence of the specified
character
• Retrieve a substring
• Working with substrings is a component of search engines and several text-processing
algorithms
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 String Functions (5 of 5)
Figure 5-17
Collecting a
substring
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.2: Discussion
1. Examine a list of the 8-bit codes for ASCII characters and their decimal equivalents (e.g., at
ASCII Table). How might this assignment of characters to codes affect how computer
programs compare or sort characters and strings?
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.4 Concatenation and Typecasting (1 of 4)
• Concatenated output
• Concatenation: the process of chaining two or more values in sequence
• The + sign is the concatenation operator
• Sample code that concatenates strings:
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
print("Hello,", first_name)
print("You are filed as", last_name + ", " + first_name)
Hello, Philip
You are filed as Parker, Philip
• Concatenated variables
• You can concatenate one or more variables and store the combined string in a new
variable
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.3: Breakout Groups:
Silly Strings
1. Form student pairs or groups.
2. To practice storing strings as variables and concatenating strings, write a Python program
that:
a. Asks the user to input three words of specified parts of speech (e.g., noun, adjective)
and then
b. Outputs them within a silly sentence, as a silly band name, or in some other silly form.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.4 Concatenation and Typecasting (2 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.4 Concatenation and Typecasting (3 of 4)
Figure 5-19
Typecasting:
Integer to string
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.4 Concatenation and Typecasting (4 of 4)
Figure 5-20
Typecasting:
String to integer
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.4: Knowledge Check
1. The Python _____ returns the number of characters in a string stored within a variable that is passed in to it
as a parameter.
2. The two values you supply when writing a Python statement to retrieve a substring are the starting index and
_____ of the substring.
3. To combine the strings in two or more variables together to produce a new string that is either output or
stored in a variable, you use the process of _____.
4. In Python, when the letters variable has the string value of "26", you can use the statement number =
_____(letters) to assign the numeric value of 26 to number.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.4: Knowledge Check Answers
1. The Python _____ returns the number of characters in a string stored within a variable that is passed in to it as a
parameter.
2. The two values you supply when writing a Python statement to retrieve a substring are the starting index and
_____ of the substring.
3. To combine the strings in two or more variables together to produce a new string that is either output or stored in
a variable, you use the process of _____.
Answer: concatenation
4. In Python, when the letters variable has the string value of "26", you can use the statement number =
_____(letters) to assign the numeric value of 26 to number.
Answer: int
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.5: Breakout Groups:
Quick Name Trivia
1. Form pairs or groups of students.
2. To practice working with characters, strings, and type casting, write a Python program that
a. Asks for the user’s name and then
b. Tells the user what letter that name begins with,
c. Tells the user how many letters are in the name, and
d. Tells the user how many letters it would take to write the name five times in a row.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.6: Discussion
1. Imagine you would like to write a program that mixes up either the words in a sentence or
the characters in a single word and returns the mixed-up version. Which string operations
and manipulation techniques described in Module 5 could you use to accomplish this task?
2. Write an algorithm for the program option you chose for the previous question in either
narrative or pseudocode form.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment
1. How are strings used in some of the computer applications you use regularly? Can you
identify use cases for the string manipulation methods and functions you learned in this
module within that context?
2. Some programming languages have a separate data type just for single characters (strings
of length 1). As a software developer, do you think you would prefer using two separate
data types for characters and strings, or simply using string data as you do in Python?
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.