CMPUT 101
Introduction to Computing
Programming in Python
Strings
Marianne Morris
[email protected]
How to use the audio and
the slide animations together
− Please turn up your device speakers and
run the slides in presentation mode
− Click on the play button of the audio
icon as soon as a new slide is up
− The play button of the audio icon will
appear when you wave the mouse over
the audio icon and will look like the
following picture:
2
How to use the audio and
the slide animations together
− Slide animations run automatically in a timed
sequence regardless of whether the audio is
played or not
− However, the sequence of animations will time
well with the audio explanations if you play the
audio immediately after a new slide is up
− Do not click the mouse and do not press the
down arrow key while the audio is playing, in
order not to skip the sequence of animations
timed with the audio 3
Strings
Strings are made of one or more
characters including the white
space
Examples:
# string of a single character
mystr = "a" # alphabetical string
fruit = "apple" # alphanumeric string
course = "CMPUT101"# numerical string
year = "2025" 4
Strings
String operations include:
Repeating a string
Concatenating strings together
Indexing
Slicing
Obtaining the length of a string
Strings comparison
Iterating over a string
5
Strings in memory
Each character in the string
occupies one byte and has an index
All strings start at index 0
The last index is the length of the
string -1
indexes
e.g., the
0
last
1
index
2
is3 7 in4a string
5
of
6
size7
8
Characters
c m p u t 1 0 1
6
String repetition
The repetition operator is the *
Example
print("ab" * 5) Output:
ababababab
Note: repetition doesn’t add a white space in the output string
7
String repetition
To add white spaces in the output
string, the white space can be
added in the original string
Output:
print("ab " * 5) ab ab ab ab
ab
Output:
print(" cd" * 4) cd cd cd
cd
8
String concatenation
The concatenation operator is
the +
Example
Output:
course = "cmput" cmput101
number = "101"
print(course + number)
Note: concatenation doesn’t add a white space between strings
9
String concatenation
We can concatenate white spaces
together with other strings
course = "cmput" Output:
cmput
number = "101" 101
print(course + " " + number)
Note the white space between the quotes
10
Strings indexing
Using the index operator [ ] to
retrieve a single character from a
string
Output:
name = "cmput" c
t
firstChar = name[0]
print(firstChar)
Using negative indexes retrieves
lastChar = name[-1]
characters from the end of the string
print(lastChar) 11
Strings size
We can use the length built-in
function len to obtain the size of a
string
The length of the string is how many
characters it contains
Output:
8
name = "cmput101"
print(len(name))
12
Strings indexing
We can retrieve the last character
of a string using the [] operator:
name = "Daniella" o Why is there an error?
o What’s the last index?
size = len(name) o What’s size?
last = name[size]
print(last)
IndexError: string index out of range on line 3
13
Strings indexing
String indexes start at 0
The first character is at index 0
first = name[0]
The last character is at index
len(name) – 1
To obtain the last character
last = name[len(name) – 1]
14
Strings slicing
We can use the slicing operator [i:j]
to retrieve one character or a
substring
Output:
c
course = "cmput101" cmput
101
print(course[0:1])
print(course[0:5])
print(course[5:8])
15
Strings comparison
To compare two strings, we use the
relational operators
< , <=, > , >=, ==, !=
Comparison follows the alphabetical
order of words in the dictionary
print("bee" < "bird") Output:
True
print("apple" > "pear") False
True
print("sun" != "moon") True
print("star" == "star")
16
Strings ordinals
We use the built-in ord function to
obtain the ordinal value (the decimal
encoding) from the ASCII table for a
single character Output:
The ordinal of a = 97
The ordinal of A = 65
Example:
print("The ordinal of a =", ord('a'))
print("The ordinal of A =", ord('A'))
ASCII Table
17
Strings immutability
Strings are immutable, i.e., once
initialized the string cannot be
modified
Attempts to modify a string will
generate the following error
TypeError: 'str' does not support item assignment
18
Strings immutability
Example of code that generates
TypeError
greeting = "Hello"
greeting[0] = 'J' # error
print(greeting)
19
Strings traversal
We can traverse a string using for
loops
Output:
M
name = "Mary" a
r
for i in range(len(name)): y
print(name[i])
Note the use of the range function and the length of the string
20
Strings traversal
Another example of traversing a
string with for loops
Output:
M
name = "Mary" a
r
for char in name: y
print(char)
Note the use of the in operator
21
Strings traversal
We can also iterate over a string
using while loops
name = "Mary" Output:
M
index = 0 a
r
while index < len(name): y
print(name[index])
index = index + 1
Note the variable index must be incremented
22
Strings: character
membership
We can check for membership
using the in and not in operators
print('a' in "abc") Output:
True
print('e' not in "abc") True
False
print('d' in "abc")
23
Strings example
Using the ord function and for loop
name = "cmput" i name Output:
ord(name[ sum
sum = 0 [i] i])
c = 99
for i in range(len(name)): 0 c 99
m = 109
99
1 m p= 109
112 208
print(name[i], "=",
2 p u= 117
112 320
ord(name[i])) t = 116
3 u 117 437
sum = sum + cmput = 553
4 t 116 553
ord(name[i])
print(name, "=", sum)
This program has practical use in efficiently searching for strings.
24
Text readings
This lecture covered
Chapter 9 of the e-text
Read the e-text
Do the activities and exercises
provided in the e-text to
strengthen your understanding of
string concepts
25