Week 4
Week 4
We can compare two strings for their dictionary order, comparing them letter by
letter:
>>> 'abracadabra' < 'ace'
True
>>> 'abracadabra' > 'ace'
False
>>> 'a' <= 'a'
True
>>> 'A' < 'B'
True
Capitalization matters, and capital letters are less than lowercase letters:
>>> 'a' != 'A'
True
>>> 'a' < 'A'
False
Every letter can be compared:
>>> ',' < '3'
True
We can compare a string and an integer for equality:
>>> 's' == 3
False
We can't compare values of two different types for ordering:
>>> 's' <= 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>>
TypeError: unorderable types: str() <= int()
The operator in checks whether a string appears anywhere inside another one (that
is, whether a string is a substring of another).
>>> 'c' in 'aeiou'
False
>>> 'cad' in 'abracadabra'
True
>>> 'zoo' in 'ooze'
False
Summary
Lecture 2
An index is a position within the string. Positive indices count from the left-hand
side with the first character at index 0, the second at index 1, and so on. Negative
indices count from the right-hand side with the last character at index -1, the
second last at index -2, and so on. For the string "Learn to Program", the indices
are:
The first character of the string is at index 0 and can be accessed using this bracket
notation:
>>> s[0]
'L'
>>> s[1]
'e'
Negative indices are used to count from the end (from the right-hand side):
>>> s[-1]
'm'
>>> s[-2]
'a'
Slicing
We can extract more than one character using slicing. A slice is a substring from
the start index up to but not including the end index. For example:
>>> s[0:5]
'Learn'
>>> s[6:8]
'to'
>>> s[9:16]
'Program'
More generally, the end of the string can be represented using its length:
>>> s[9:len(s)]
'Program'
The end index may be omitted entirely and the default is len(s):
>>> s[9:]
'Program'
Similarly, if the start index is omitted, the slice starts from index 0:
>>> s[:]
'Learn to Program'
>>> s[:8]
'Learn to'
Negative indices can be used for slicing too. The following three expressions are
equivalent:
>>> s[1:8]
'earn to'
>>> s[1:-8]
'earn to'
>>> s[-15:-8]
'earn to'
Modifying Strings
The slicing and indexing operations do not modify the string that they act on, so
the string that s refers to is unchanged by the operations above. In fact, we cannot
change a string. Operations like the following result in errors:
>>> s[6] = 'd'
Traceback (most recent call last):
File <"pyshell#19", line 1, in <module>
s[6] = 'd'
TypeError: 'str' object does not support item assignment
Imagine that we want to change string s to refer to 'Learned to Program'. The
following expression evaluates to that 'Learned to Program': s[:5] + 'ed' +
s[5:]
Notice that the string that s originally referred to was not modified: strings cannot
be modified. Instead a new string was created and s was changed to point to that
string.
LECTURE 2
String Methods
To get information about a method, such as the lower method, do the following:
>>> help(str.lower)
LECTURE 3:
The variable refers to each character of the string in turn and executes the body of
the loop for each character. For example:
>>> s = 'yesterday'
>>> for char in s:
... print(char)
...
y
e
s
t
e
r
d
a
y
num_vowels = 0
for char in s:
if char in 'aeiouAEIOU':
num_vowels = num_vowels + 1
return num_vowels
The loop in the function above will loop over each character that s refers to, in
turn. The body of the loop is executed for each character, and when a character is a
vowel, the if condition is True and the value that num_vowels refers to is increased
by one.
def collect_vowels(s):
""" (str) -> str
vowels = ''
for char in s:
if char in 'aeiouAEIOU':
vowels = vowels + char
return vowels
Variable vowels initially refers to the empty string, but over the course of the
function it accumulates the vowels from s