7.
String Methods
Topics:
Methods and Data
More on Strings
Functions and Methods
The String Class
Data + Functions Together
“The square root of nine is three.”
The tone of this comment is that the
square root function can be applied to
numbers like nine.
“Three is nine’s square root.” A
The tone of this comment is that new
point
the number nine (like all numbers) of
comes equipped with a sqrt function. view
Methods
A special kind of function that is very important
to object-oriented programming is called a
method.
In this style of programming, there is a tight
coupling between structured data and the
methods that work with that data.
Methods
Hard to appreciate the reasons for this
coupling between data and methods so early in
the course.
For now, we settle on getting used to the
special notation that is associated with the
use of methods.
We will get into this topic using strings.
Three String Methods
count How many times does string t
occur in a string s?
find Where is the first occurrence of
string t in a string s?
replace In a string s replace all occurrences
of a string s1 with a string s2.
There will be others later in the course.
Designing count as a Function
count How many times does string y
occur in a string x?
‘ITH-JFK-ITH’
count 2
‘ITH’
It would then be used like this: n = count(y,x)
Designing count as a Method
Suppose
x = ‘ITH-JFK-ITH’ y =
‘ITH’
Instead of the usual function-call syntax
n = count(y,x)
we will write
n = [Link](y)
Methods: The Notation
Here is the syntax associated with using
a string method:
name of string name of method (arg1,arg2,…)
Once again, the ‘dot” notation
String Methods: count
>>> s =‘ITH-JFK-ITH’
>>> m = [Link](‘ITH’)
s --> I T H - J F K - I T H
0 1 2 3 4 5 6 7 8 9 10
m --> 2
[Link](s2) the number of occurrences of string s2 in string s1
String Methods: count
>>> s =‘ITH-JFK-ITH’
>>> m = [Link](‘LGA’)
s --> I T H - J F K - I T H
0 1 2 3 4 5 6 7 8 9 10
m --> 0
[Link](s2) the number of occurrences of string s2 in string s1
count
The Formal Definition
If s1 and s2 are strings, then
[Link](s2)
returns an int value that is the number of
occurrences of string s2 in string s1.
Note, in general [Link](s2) is not the same as [Link](s1)
Using count: An Example
# Count the number of vowels…
A=‘auric goldfinger’
n = 0
n = n + [Link](‘a’)
n = n + [Link](‘e’)
n = n + [Link](‘i’)
n = n + [Link](‘o’)
n = n + [Link](‘u’)
print(n)
Illegal: n = [Link](‘a’ or ‘e’ or ‘I’ or ‘o’ or ‘u’)
Designing find as a Function
find Where is the first occurrence of string y
in a string x?
‘ITH-JFK-ITH’
find 3
‘-’
It would then be used like this: n = find(y,x)
Designing find as a Method
>>> s =‘ITH-JFK-ITH’
>>> idx = [Link](‘JFK’)
s --> I T H - J F K - I T H
0 1 2 3 4 5 6 7 8 9 10
idx --> 4
[Link](s2) the index of the first occurrence of string s2 in string s1
String Methods: find
>>> s =‘ITH-JFK-ITH’
>>> idx = [Link](‘RFK’)
s --> I T H - J F K - I T H
0 1 2 3 4 5 6 7 8 9 10
idx --> -1
[Link](s2) evaluates to -1 if there is no occurrence of s2 in s1
find
The Formal Definition
If s1 and s2 are strings, then
[Link](s2)
returns an int value that is the index of the
first occurrence of string s2 in string s1.
If there is no such occurrence, then the value -1
is returned.
Using find : Some Examples
s = ‘nine one one’
n1 = [Link](‘one’)
n2 = [Link](‘two’)
n3 = [Link](‘ nine’)
n1 -> 5 n2 -> -1 n3 -> -1
in : A Handy Boolean Device
If s1 and s2 are strings, then
s1 in s2
is a boolean-valued expression.
True if there is an instance of s1 in s2.
False if there is NOT an instance of s1 in s2.
in versus find
These are equivalent:
x = s1 in s2
x = [Link](s1)>=0
Designing replace as a Function
replace In a string s replace all occurrences
of a string s1 with a string s2.
‘ITH-JFK-ITH’
‘ITH’ replace ‘??-JFK-??’
‘??’
It would then be used like this: sNew = replace(s,s1,s2)
Designing replace as a Method
s = ‘one hundred and one’
t = [Link](‘ ’,’-’)
s -> ‘one hundred and one’
t -> ‘one-hundred-and-one’
Replacing one character with another
The replace Method
s = ‘one hundred and one’
t = [Link](‘ ’,‘’)
s -> ‘one hundred and one’
t -> ‘onehundredandone’
The null string
has length 0.
Replacing each blank with the “null string”
The replace Method
s = ‘one hundred and one’
t = [Link](‘x’,‘-’)
s -> ‘one hundred and one’
t -> ‘one hundred and one’
No change if the character to be replaced is missing
The replace Method
s = ‘one hundred and one’
t = [Link](‘one’,‘seven’)
s -> ‘one hundred and one’
t -> ‘seven hundred and seven’
Replacing one substring with another
The replace Method
s = ‘one hundred and one’
t = [Link](‘two’,’seven’)
s -> ‘one hundred and one’
t -> ‘one hundred and one’
No change if the designated substring is missing
replace
The Formal Definition
If s, s1 and s2 are strings, then
[Link](s1,s2)
returns a copy of the string s in which every
non- overlapping occurrence of the string s1 is
replaced by the string s2.
If s1 is not a substring of s, then the returned
string is just a copy of s.
Using replace : Some Examples
s = ‘xxx’
t1 = [Link](‘x’,‘o’)
t2 = [Link](‘xx’,‘o’)
t3 = [Link](‘xx’,‘oo’)
t1 -> ‘ooo’
t2 -> ‘ox’
t3 -> ‘oox’
replace does Not Replace
[Link](s1,s2) does not change the value of
s.
It produces a copy of s with the specified
replacements.
You are allowed to overwrite the “original” s
with the its “updated” copy:
s = [Link](s1,s2)
Illegal!
s = ‘abcdefgh’
s[5] = ‘x’
Strings are immutable. They cannot be
changed.
Have to ``live with’’ the replace function,
slicing, and concatenation
s = ‘abcdefgh’
s = s[:5]+’x’+s[6:]
Quickly Review Some
Other String Methods
The upper and lower Methods
s = ‘A2sh?’ s -> ‘A2sh?’
t1 = [Link]() t1 -> ‘A2SH?’
t2 = [Link]() t2 -> ‘a2sh?’
Some Boolean-Valued Methods
These methods return either True or False:
islower()
isupper()
isalnum()
isalpha()
isdigit()
Boolean-Valued Methods
s=‘ab3?’ s=‘AbcD’ s=‘AB3’
[Link]() True False False
[Link]() False False True
Boolean-Valued Methods
‘23’ ‘5a7’ ‘ab’ ‘-2.3’
[Link]() True True True False
[Link]() False False True False
[Link]() True False False False
Useful String Constants
alpha = [Link]
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Useful String Constants
specialChar = [Link]
!"#$%&'()*+,./:;<=>?@[\]^_`{|}~
Useful String Constants
TheDigits = [Link]
1234567890
The “Dot” Notation--Again
We have seen it with modules and import
[Link]
[Link] [Link]
pi=3.1416 The “folder metaphor.
sqrt The “dot” means “go inside
and get this”
string is a “Special” Module
“[Link]”
The “folder”
digits = ‘01234567890’
metaphor.
letters = ‘abcdef etc
The “dot” means
punctuation =‘!"#$ etc
“go inside and
count isupper isalnum get this”
find islower isalpha string is actually
a “class”. More
replace isdigit
in a few lectures.