String Methods

In addition to expression operators, strings provide a set of methods that implement more sophisticated text-processing tasks. Methods are simply functions that are associated with particular objects. Technically, they are attributes attached to objects that happen to reference callable functions. In Python, expressions and built-in functions may work across a range of types, but methods are generally specific to object types—string methods, for example, work only on string objects. The method sets of some types intersect in Python 3.0 (e.g., many types have a count method), but they are still more type-specific than other tools.

In finer-grained detail, functions are packages of code, and method calls combine two operations at once (an attribute fetch and a call):

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

Attribute fetches

An expression of the form object.attribute means “fetch the value of attribute in object.”

Call expressions

An expression of the form function(arguments) means “invoke the code of function, passing zero or more comma-separated argument objects to it, and return function’s result value.”

Putting these two together allows us to call a method of an object. The method call expression object.method(arguments) is evaluated from left to right—Python will first fetch the method of the object and then call it, passing in the arguments. If the method computes a result, it will come back as the result of the entire method-call expression.

As you’ll see throughout this part of the book, most objects have callable methods, and all are accessed using this same method-call syntax. To call an object method, as you’ll see in the following sections, you have to go through an existing object.

Table 7-3 summarizes the methods and call patterns for built-in string objects in Python 3.0; these change frequently, so be sure to check Python’s standard library manual for the most up-to-date list, or run a help call on any string interactively. Python 2.6’s string methods vary slightly; it includes a decode, for example, because of its different handling of Unicode data (something we’ll discuss in Chapter 36). In this table, S is a string object, and optional arguments are enclosed in square brackets. String methods in this table implement higher-level operations such as splitting and joining, case conversions, content tests, and substring searches and replacements.

Table 7-3. String method calls in Python 3.0

S.capitalize()

S.ljust(width [, fill])

S.center(width [, fill])

S.lower()

S.count(sub [, start [, end]])

S.lstrip([chars])

S.encode([encoding [,errors]])

S.maketrans(x[, y[, z]])

S.endswith(suffix [, start [, end]])

S.partition(sep)

S.expandtabs([tabsize])

S.replace(old, new [, count])

S.find(sub [, start [, end]])

S.rfind(sub [,start [,end]])

S.format(fmtstr, *args, **kwargs)

S.rindex(sub [, start [, end]])

S.index(sub [, start [, end]])

S.rjust(width [, fill])

S.isalnum()

S.rpartition(sep)

S.isalpha()

S.rsplit([sep[, maxsplit]])

S.isdecimal()

S.rstrip([chars])

S.isdigit()

S.split([sep [,maxsplit]])

S.isidentifier()

S.splitlines([keepends])

S.islower()

S.startswith(prefix [, start [, end]])

S.isnumeric()

S.strip([chars])

S.isprintable()

S.swapcase()

S.isspace()

S.title()

S.istitle()

S.translate(map)

S.isupper()

S.upper()

S.join(iterable)

S.zfill(width)

As you can see, there are quite a few string methods, and we don’t have space to cover them all; see Python’s library manual or reference texts for all the fine points. To help you get started, though, let’s work through some code that demonstrates some of the most commonly used methods in action, and illustrates Python text-processing basics along the way.