Lec06 Strings - T
Lec06 Strings - T
Introduction to Computing
Programming in Python
Strings
Marianne Morris
marims@ualberta.ca
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
print("ab" * 5) Output:
ababababab
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
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:
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
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)
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