Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Programming with Python

Module 5: Character and String


Data Types

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)

• 5.2 String Data Type (continued)


• 5.2.4 Specify when an escape sequence is necessary.
• 5.2.5 Describe the memory allocation for strings.
• 5.2.6 Identify the index values for characters in a string.
• 5.3 String Functions
• 5.3.1 List commonly used functions and methods that programming languages provide to
manipulate strings.
• 5.3.2 Identify code that produces the length of a string.
• 5.3.3 Identify code that changes the case of a string.
• 5.3.4 Explain the significance of case sensitivity.
• 5.3.5 Provide use cases for finding a character in a string.
• 5.3.6 Explain the general approach to retrieving substrings.

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)

• 5.4 Concatenation and Typecasting


• 5.4.1 Explain the meaning of concatenation, and identify the concatenation operator.
• 5.4.2 Provide an example of when a programmer would concatenate a string.
• 5.4.3 Concatenate a string during output.
• 5.4.4 Concatenate the contents of multiple variables.
• 5.4.5 State which data types can be successfully concatenated.
• 5.4.6 Explain the meanings of coercion and typecasting in the context of data types.

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)

• Working with character data


• Character: classification used for variables that hold a single letter of the alphabet, a
numeral from 0 to 9, or a symbol
• Sample assignment of a character literal to a variable:
first_letter = "a"

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)

Figure 5-2 Character


data initialization and
storage

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)

• Character memory allocation


• ASCII (American Standard Code for Information Interchange): encoding method that
assigns a unique sequence of 8 bits to each character
• Each of the uppercase letters, lowercase letters, numerals, and symbols on your
computer keyboard has a unique ASCII value
• E.g., uppercase A is ASCII code 01000001, lowercase a is 01100001
• Digits
• The double quotes around strings composed of the digits 0 to 9 indicate they are strings,
not integers
• The 5 in a_digit = "5" is stored as an ASCII value
• The 5 in an_integer = 5 is stored as a binary number
• Character output format
• Digits can be separated by including white space in the print statement

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)

Figure 5-6 Inserting a space in the


output stream

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)

Figure 5-7 Output to separate lines

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)

Character Example Data Result


Function/Method
Check if the character is a initial.isalpha() initial = "a" True
letter of the alphabet choice.isalpha() choice = "2" False
Check if the character is a initial.isdigit() initial = "a" False
digit choice.isdigit() choice = "2" True
Change to uppercase initial.upper() initial = "a" A
Change to lowercase initial.lower() initial = "A" a

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.2 String Data Type (1 of 3)

• Working with string data


• String: a sequence of alphanumeric characters
• The string data type is used for working with variables containing words, phrases,
sentences, and other text
• Sample code for working with strings:
your_name = "" (initializing an empty string)
message = "likes alphabet soup." (initializing a string variable)
print("What is your name? ") (displaying a prompt)
your_name = input(). (collecting user input)
print(your_name, message). (displaying two strings)

What is your name?


Gaius Julius Caesar
Gaius Julius Caesar likes alphabet soup.

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)

Figure 5-13 Each character in a string


is stored in a memory location
referenced by an index

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

1. Character data is stored in _____ format.

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) _____.

4. Within the string "apple", the character e is located at index _____.

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

1. Character data is stored in _____ format.

Answer: ASCII (American Standard Code for Information Interchange)

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: escape sequence

4. Within the string "apple", the character e is located at index _____.

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)

Figure 5-15 Finding the length of a


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.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.

find() Returns the first index of the "apple".find("p") 1


specified value or –1 if it can’t
be found.

startswith() Returns True if the string "caterpillar".startswith("cat") True


starts with the specified value.

title() Capitalizes the first letter of "hello world!".title() Hello


each word in the string. World!

Figure 5-16 Some string methods

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)

• Coercion and typecasting


• With most programming languages, strings can be concatenated with other strings and
character data, but not with integer or floating-point data
• To fix an integer-string type mismatch, use typecasting to convert the integer to a string
or vice-versa
• You can use the str() and int() functions to perform these typecasting operations

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.

Answer: len() function

2. The two values you supply when writing a Python statement to retrieve a substring are the starting index and
_____ of the substring.

Answer: ending index

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

Click the link to review the objectives for this presentation.


Link to Objectives

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.

You might also like