Marquette University, view of Wisconsin Avenue  

Module 12

Strings

We already have printed out strings in Python. They can be defined by using quotation marks (double or single). Python strings are immutable, which means that they cannot be changed in place. The reason is that real-life programs have all manner of strings such as captions of buttons, etc., sometimes in many different languages, and that immutability implies that a Python program can share these strings. More importantly, Python uses hashing for its fast data structures, and the hash of something immutable stays the same. Immutability means that if we write string1 += "!!!", a new copy of string1 is generated, using up more memory resources. For this reason, in this class, the use of operations on strings is discouraged. You will find much code on the net that builds strings incrementally, but usually not by experienced Python programmers.

Python has three different ways to define a Python string literal, namely single quotes, double quotes, and triple double-or-single quotes. The latter allows the use of newlines directly in the text.

To access single letters in a string, we use the same notation as for lists. If astring is a string, then we can access its last letter via astring[-1].

A frequent pattern in this class is the changing of a string. For example, we might want to replace all vowels in the string by numbers, a not-so-secure ploy to change a dictionary word into a word that is not in the dictionary. To do so, we first define an initially empty list that contains the letters of the string to be returned. We then use a for loop in order to walk through the string, processing each letter in turn. During the processing we usually append a letter or a short string to the results list. When we are done, we use the "".join(results_list) to convert the results list to a string.