1/22/2016
Data + Functions Together
7. String Methods
“The square root of ni ne i s three.”
The tone of thi s comment i s that the
Topics: square root functi on can be appli ed to
Methods and Data numbers li ke ni ne.
More on Strings
Functions and Methods “Three i s ni ne’ s square root.” A
The String Class The tone of thi s comment i s that new
point
the number ni ne (li ke all numbers) of
comes equi pped w i th a sqrt functi on. view
Methods Methods
Hard to appreci ate the reasons for thi s coupli ng
A speci al ki nd of functi on that i s very i mportant betw een data and methods so early i n the
to object-ori ented programmi ng i s called a course.
method.
For now , w e settle on getti ng used to the
In thi s style of programmi ng, there i s a ti ght speci al notati on that i s associ ated w i th the
coupli ng betw een structured data and the use of methods.
methods that w ork w i th that data.
We w i ll get i nto thi s topi c usi ng stri ngs.
Three String Methods Designing count as a Function
count How many ti mes does stri ng t count How many ti mes does stri ng y
occur i n a stri ng s? occur i n a stri ng x?
find Where i s the fi rst occurrence of
stri ng t i n a stri ng s? ‘ITH-JFK-ITH’
count 2
replace In a stri ng s replace all occurrences ‘ITH’
of a stri ng s1 w i th a stri ng s2.
There will be others later in the course. It would then be used like this: n = count(y,x)
1
1/22/2016
Designing count as a Method Methods: The Notation
Suppose
x = ‘ITH-JFK-ITH’ Here is the syntax associated with using
y = ‘ITH’ a string method:
Instead of the usual functi on-call syntax
name of string name of method (arg1,arg2,…)
n = count(y,x)
w e w i ll w rite
Once agai n, the ‘ dot” notati on
n = x.count(y)
String Methods: count String Methods: count
>>> s =‘ITH-JFK-ITH’ >>> s =‘ITH-JFK-ITH’
>>> m = s.count(‘ITH’) >>> m = s.count(‘LGA’)
s --> I T H - J F K - I T H s --> I T H - J F K - I T H
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
m --> 2 m --> 0
s1.count(s2) the number of occurrences of string s2 in string s1 s1.count(s2) the number of occurrences of string s2 in string s1
count Using count: An Example
The Formal Definition
# Count the number of vowels…
A = ‘auric goldfinger’
If s1 and s2 are stri ngs, then n = 0
n = n + A.count(‘a’)
s1.count(s2) n = n + A.count(‘e’)
n = n + A.count(‘i’)
returns an i nt value that i s the number of n = n + A.count(‘o’)
occurrences of stri ng s2 i n stri ng s1. n = n + A.count(‘u’)
print n
Note, in general s1.count(s2) is not the same as s2.count(s1) Illegal: n = A.count(‘a’ or ‘e’ or ‘I’ or ‘o’ or ‘u’)
2
1/22/2016
Designing find as a Function Designing find as a Method
find Where i s the fi rst occurrence of >>> s =‘ITH-JFK-ITH’
stri ng y i n a stri ng x? >>> idx = s.find(‘JFK’)
‘ITH-JFK-ITH’ s --> I T H - J F K - I T H
find 3 0 1 2 3 4 5 6 7 8 9 10
‘-’
idx --> 4
It would then be used like this: n = find(y,x) s1.index(s2) the index of the first occurrence of string s2 in string s1
String Methods: find find
The Formal Definition
>>> s =‘ITH-JFK-ITH’
>>> idx = s.find(‘RFK’) If s1 and s2 are stri ngs, then
s1.find(s2)
s --> I T H - J F K - I T H
returns an i nt value that i s the i ndex of the
0 1 2 3 4 5 6 7 8 9 10 fi rst occurrence of stri ng s2 i n stri ng s1.
idx --> -1 If there i s no such occurrence, then the value -1
i s returned.
s1.index(s2) evaluates to -1 if there is no occurrence of s2 in s1
Using find : Some Examples in : A Handy Boolean Device
If s1 and s2 are stri ngs, then
s = ‘nine one one’
n1 = s.find(‘one’)
s1 in s2
n2 = s.find(‘two’)
n3 = s.find(‘ nine’)
i s a boolean-valued expressi on.
True i f there i s an i nstance of s1 i n s2.
n1 -> 5 n2 -> -1 n3 -> -1 False i f there i s NOT an i nstance of s1 i n s2.
3
1/22/2016
in versus find Designing replace as a Function
replace In a stri ng s replace all occurrences
of a stri ng s1 w i th a stri ng s2.
These are equi valent:
x = s1 in s2 ‘ITH-JFK-ITH’
‘ITH’ replace ‘??-JFK-??’
x = s2.find(s1)>=0 ‘??’
It would then be used like this: sNew = replace(s,s1,s2)
Designing replace as a Method The replace Method
s = ‘one hundred and one’ s = ‘one hundred and one’
t = s.replace(‘ ’,’-’) t = s.replace(‘ ’,‘’)
s -> ‘one hundred and one’ s -> ‘one hundred and one’
t -> ‘one-hundred-and-one’ t -> ‘onehundredandone’
The null string
has length 0.
Replacing one character with another Replacing each blank with the “null string”
The replace Method The replace Method
s = ‘one hundred and one’ s = ‘one hundred and one’
t = s.replace(‘x’,‘-’) t = s.replace(‘one’,‘seven’)
s -> ‘one hundred and one’ s -> ‘one hundred and one’
t -> ‘one hundred and one’ t -> ‘seven hundred and seven’
No change if the character to be replaced is missing Replacing one substring with another
4
1/22/2016
The replace Method replace
The Formal Definition
s = ‘one hundred and one’ If s, s1 and s2 are stri ngs, then
t = s.replace(‘two’,’seven’)
s.replace(s1,s2)
s -> ‘one hundred and one’ returns a copy of the stri ng s i n w hi ch every
non- overlappi ng occurrence of the stri ng s1 i s
t -> ‘one hundred and one’ replaced by the stri ng s2.
If s1 i s not a substri ng of s, then the returned
stri ng i s just a copy of s.
No change if the designated substring is missing
Using replace : Some Examples replace does Not Replace
s = ‘xxx’ s.replace(s1,s2) does not change the value
t1 = s.replace(‘x’,‘o’) of s.
t2 = s.replace(‘xx’,‘o’)
It produces a copy of s w i th the speci fi ed
t3 = s.replace(‘xx’,‘oo’)
replacements.
t1 -> ‘ooo’ You are allow ed to overw ri te the “ori gi nal” s
w i th the i ts “updated” copy:
t2 -> ‘ox’
s = s.replace(s1,s2)
t3 -> ‘oox’
Illegal!
s = ‘abcdefgh’
s[5] = ‘x’
Stri ngs are i mmutable. They cannot be
Quickly Review Some
changed. Other String Methods
Have to ``li ve w i th’ ’ the replace functi on,
sli ci ng, and concatenati on
s = ‘abcdefgh’
s = s[:5]+’x’+s[6:]
5
1/22/2016
The upper and lower Methods Some Boolean-Valued Methods
These methods return ei ther True or False:
s = ‘A2sh?’ s -> ‘A2sh?’ islower()
t1 = s.upper() t1 -> ‘A2SH?’ isupper()
t2 = s.lower() t2 -> isalnum()
‘a2sh?’
isalpha()
isdigit()
Boolean-Valued Methods Boolean-Valued Methods
s=‘ab3?’ s=‘AbcD’ s=‘AB3’
‘23’ ‘5a7’ ‘ab’ ‘-2.3’
s.islower() True False False
s.isalnum() True True True False
s.isupper() False False True
s.isalpha() False False True False
s.isdigit() True False False False
Useful String Constants Useful String Constants
specialChar = string.punctuation
alpha = string.letters
!"#$%&'()*+,./:;<=>?@[\]^_`{|}~
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
6
1/22/2016
Useful String Constants The “Dot” Notation--Again
We have seen i t w i th modules and i mport
TheDigits = string.digits
math.sqrt
math.py math.pi
1234567890
pi=3.1416 The “folder metaphor.
sqrt
The “dot” means “go i nsi de
and get thi s”
string is a “Special” Module
“string.py”
The “folder”
digits = ‘01234567890’
metaphor.
letters = ‘abcdef etc
The “dot” means
punctuation =‘!"#$ etc “go i nsi de and
get thi s”
count isupper isalnum
find islower isalpha string is actually
a “class”. More
replace isdigit in a few lectures.