Notice that in the prior examples, we were not changing the original string with any of the operations we ran on it. Every string operation is defined to produce a new string as its result, because strings are immutable in Python—they cannot be changed in-place after they are created. For example, you can’t change a string by assigning to one of its positions, but you can always build a new one and assign it to the same name. Because Python cleans up old objects as you go (as you’ll see later), this isn’t as inefficient as it may sound:
>>> S = 'z' + S[1:] # But we can run expressions to make new objects
Every object in Python is classified as immutable (unchangeable) or not. In terms of the core types, numbers, strings, and tuples are immutable; lists and dictionaries are not (they can be changed in-place freely). Among other things, immutability can be used to guarantee that an object remains constant throughout your program.
Every string operation we’ve studied so far is really a sequence operation—that is, these operations will work on other sequences in Python as well, including lists and tuples. In addition to generic sequence operations, though, strings also have operations all their own, available as methods (functions attached to the object, which are triggered with a call expression).
For example, the stringfindmethod is the basic substring search operation (it returns the offset of the passed-in substring, or-1if it is not present), and the stringreplacemethod performs global searches and replacements:
>>> S.find('pa') # Find the offset of a substring
Again, despite the names of these string methods, we are not changing the original strings here, but creating new strings as the results—because strings are immutable, we have to do it this way. String methods are the first line of text-processing tools in Python; other methods split a string into substrings on a delimiter (handy as a simple form of parsing), perform case conversions, test the content of the string (digits, letters, and so on), and strip whitespace characters off the ends of the string:
>>> line = 'aaa,bbb,ccccc,dd'
>>> S = 'spam'
>>> S.isalpha() # Content tests: isalpha, isdigit, etc.
>>> line = 'aaa,bbb,ccccc,dd\n'
One note here: although sequence operations are generic, methods are not—string method operations work only on strings, and nothing else. As a rule of thumb, Python’s toolset is layered: generic operations that span multiple types show up as built-in functions or expressions (e.g.,len(X),X), but type-specific operations are method calls (e.g.,aString.upper()). Finding the tools you need among all these categories will become more natural as you use Python more, but the next section gives a few tips you can use right now.
The methods introduced in the prior section are a representative, but small, sample of what is available for string objects. In general, this book is not exhaustive in its look at object methods. For more details, you can always call the built-in dirfunction, which returns a list of all the attributes available in a given object. Because methods are function attributes, they will show up in this list:
You probably won’t care about the names with underscores in this list until later in the book, when we study operator overloading in classes—they represent the implementation of the string object, and are available to support customization. In general, leading and trailing double underscores is the naming pattern Python uses for implementation details. The names without the underscores in this list are the callable methods on string objects.
Thedirfunction simply gives the methods’ names. To ask what they do, you can pass them to thehelpfunction:
Like S.find() but raise ValueError when the substring is not found.
helpis one of a handful of interfaces to a system of code that ships with Python known as PyDoc—a tool for extracting documentation from objects. Later in the book, you’ll see that PyDoc can also render its reports in HTML format.
You can also ask for help on an entire string (e.g.,help(S)), but you may get more help than you want to see—i.e., information about every string method. It’s generally better to ask about a specific method, as we did above.
For more details, you can also consult Python’s standard library reference manual, or commercially published reference books, butdir andhelpare the first line of documentation in Python.
blog comments powered by Disqus