Exercise 1.4
In this exercise, we experiment with operations on Python’s string type. You may want to do most of this exercise at the Python interactive prompt where you can easily see the results.
Define a string containing a series of stock ticker symbols like this:
>>> symbols = 'AAPL,IBM,MSFT,YHOO,SCO'
>>>
Now, let’s experiment with different string operations:
(a) Extracting individual characters and substrings
Strings are arrays of characters. Try extracting a few characters:
>>> symbols[0]
'A'
>>> symbols[1]
'A'
>>> symbols[2]
'P'
>>> symbols[-1] # Last character
'O'
>>> symbols[-2] # Negative indices are from end of string
'C'
>>>
(b) Strings as read-only objects
In Python, strings are read-only. Verify this by trying to change the first character of symbols
to a lower-case a.
>>> symbols[0] = 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
(c) String concatenation
Although string data is read-only, you can always reassign a variable to a newly created string.
Try the following statement which concatenates a new symbol "GOOG" to the end of symbols
:
>>> symbols = symbols + 'GOOG'
>>> symbols
'AAPL,IBM,MSFT,YHOO,SCOGOOG'
>>>
Oops! That’s not what we wanted. Let’s fix it:
>>> symbols = symbols[:-4] # All but last 4 chars
>>> symbols
'AAPL,IBM,MSFT,YHOO,SCO'
>>> symbols = symbols + ',GOOG' # Note the leading comma
>>> symbols
'AAPL,IBM,MSFT,YHOO,SCO,GOOG'
>>>
Now, try adding "HPQ" to the beginning of symbols
like this:
>>> symbols = 'HPQ,' + symbols
>>> symbols
'HPQ,AAPL,IBM,MSFT,YHOO,SCO,GOOG'
>>>
It should be noted in both of these examples, the original string
symbols
is NOT being modified "in place" (i.e., modifications
don’t overwrite the memory currently being used to to store the string
contents). Instead, a completely new string is created. The variable
name symbols
is just reassigned to the new value. Afterwards, the
old string is destroyed since it’s not being used anymore.
(d) Membership testing (substring testing)
Experiment with the in
operator to check for substrings. At
the interactive prompt, try these operations:
>>> 'IBM' in symbols
True
>>> 'AA' in symbols
True
>>> 'CAT' in symbols
False
>>>
Make sure you understand why the check for "AA" returned True
.
(e) String Methods
At the Python interactive prompt, try experimenting with some of the string methods.
>>> symbols.lower()
'hpq,aapl,ibm,msft,yhoo,sco,goog'
>>> symbols
'HPQ,AAPL,IBM,MSFT,YHOO,SCO,GOOG'
>>>
Remember, strings are always read-only. If you want to save the result of an operation, you need to place it in a variable:
>>> lowersyms = symbols.lower()
>>> lowersyms
'hpq,aapl,ibm,msft,yhoo,sco,goog'
>>>
Try some more operations:
>>> symbols.find('MSFT')
13
>>> symbols[13:17]
'MSFT'
>>> symbols = symbols.replace('SCO','DOA')
>>> symbols
'HPQ,AAPL,IBM,MSFT,YHOO,DOA,GOOG'
>>> for s in symbols:
print 's=', s
... see what happens
By the way, the for
statement is what Python uses to iterate
over the contents of something. In the case of a string, it iterates
over the individual letters—one at a time.