Lesson 11 - Using strings and sequences

advertisement
Day 3 – Lesson 11
Using strings and sequences
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Lesson objectives
1. Understand how Python stores and
uses strings
2. Perform indexing and slicing
operations on Python sequences
3. Traverse strings with a loop
4. Compare strings and substrings
2
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Strings in Python
 A string is a sequence of
characters
 Sequences are indexed
fruit = 'banana'
letter = fruit[1]
print letter
3
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Notes on indexing
 Python uses zero-based indexing
print fruit[0]
 Brackets vs. parenthesis
 Use brackets [x] for indexing
 Use parenthesis (x) for function
calls
4
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Notes on indexing
 You can use any expression as an
index, provided it has an integer
value
fruit = 'banana'
a, b = 1, 3
print fruit[b-a]
5
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Notes on indexing
 You can use any expression as an
index, provided it has an integer
value
fruit = 'banana'
a, b = 1.0, 3.0
print fruit[a-b]
6
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Notes on indexing
 Negative indices count backward
from the end of the sequence
fruit = 'banana'
print fruit[-1]
print fruit[-2]
7
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Slicing a sequence
 You can specify a range of indices
to slice a sequence
fruit = 'banana'
print fruit[1:3]
8
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Slicing a sequence
 For slicing, imagine the indices as
pointing between the characters
9
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Slicing a sequence
 To slice from the beginning of the
sequence, omit the first index
print fruit[:3]
 To slice from the end of the
sequence, omit the last index
print fruit[3:]
10
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Slicing a sequence
 What do these do?
print fruit[3:3]
print fruit[:]
11
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Mutability
 In Python, some types of
sequences can be changed
 These are mutable
 Others cannot be changed
 These are immutable
12
Python Mini-Course: Day 3 – Lesson 11
5/02/09
A mutable sequence: list
x = [1,2,3]
print x
x[1] = 4
print x
 Here, x is a list. We'll learn more
about lists next week.
13
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Are strings mutable?
x = 'perrot'
print x
x[1] = 'a'
print x
14
Python Mini-Course: Day 3 – Lesson 11
5/02/09
"Changing" a string
x = 'perrot'
x = x[:1] + 'a' + x[2:]
print x
 The + sign is a concatenation
operator for sequences
 NB: The above code actually creates
a new string and assigns it to x
15
Python Mini-Course: Day 3 – Lesson 11
5/02/09
The len function
 Syntax
len(sequence)
 Returns the number of items in a
sequence
 NB: because of zero-based indexing,
the last valid index is one less than
the length
16
Python Mini-Course: Day 3 – Lesson 11
5/02/09
The len function
 Example
length = len(fruit)
last = fruit[length]
print last
17
Python Mini-Course: Day 3 – Lesson 11
5/02/09
The len function
 Example
length = len(fruit)
last = fruit[length-1]
print last
18
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Traversing a sequence
 Often, we want to do something
to every item in a sequence
 We need to traverse the sequence
 This can be done with a loop
19
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Using a while loop: traverse1.py
def traverse(string):
index = 0
while index < len(string):
letter = string[index]
print letter
index += 1
traverse('Monty Python')
20
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Using a for loop: traverse2.py
def traverse(string):
for letter in string:
print letter
traverse('Monty Python')
21
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Searching strings: find.py
def find(word, letter):
index = 0
while index < len(word):
if word[index] == letter:
return index
index = index + 1
return -1
22
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Searching strings: count.py
def count(word, letter):
count = 0
for item in word:
if item == letter:
count += 1
return count
count('banana', 'a')
23
Python Mini-Course: Day 3 – Lesson 11
5/02/09
String comparison
 To compare whole strings, use
the standard comparison
operators
==
<
>
<=
>=
 NB: strings are compared using numeric
codes (e.g., ASCII), so case is very
important
24
Python Mini-Course: Day 3 – Lesson 11
5/02/09
String comparison
(try this on the command line of IDLE)
x, y, z = 'abc', 'Abc', 'aBc'
x == y
x < y
x > y
x < z
x > z
25
Python Mini-Course: Day 3 – Lesson 11
5/02/09
The in operator
 When used in a for statement,
in iterates through a sequence
 However, in is also a Boolean
operator that checks membership
within a sequence
'a' in 'banana'
26
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Comparing strings:
string_comp.py
def compare(string1, string2):
if string1 in string2:
print string1 + ' is a substring of ' + string2
if string2 in string1:
print string2 + ' is a substring of ' + string1
if string1 == string2:
print string1 + ' equals ' + string2
elif string1 > string2:
print string1 + ' comes after ' + string2
else:
print string1 + ' comes before ' + string2
27
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Comparing strings
compare('apple', 'banana')
compare('banana', 'Pineapple')
compare('banana', 'ana')
compare('banana', 'banana')
28
Python Mini-Course: Day 3 – Lesson 11
5/02/09
Download