You can also use the name str to access methods defined in the str class, such as str.capitalize:
| | >>> str.capitalize |
| | <method 'capitalize' of 'str' objects> |
Methods are like
functions, but the first
argument is an object
Methods differ from regular functions in that they are tied to a specific object. Every method in class str requires a string as the first argument. Python provides a “dot” notation for calling a method where the object appears first, followed by the method call with any additional parameters:
| | >>> 'browning'.capitalize() # same as str.capitalize('browning') |
| | 'Browning' |
| | >>> 'Sonnet 43'.center(26) # same as str.center('Sonnet 43', 26). |
| | ' Sonnet 43 ' |
| | >>> 'How do I love thee? Let me count the ways.'.count('the') |
| | 2 |
When you call a string method like ’browning’.capitalize, the string ’browning’ is automatically passed as the first parameter to the method. There’s no need to pass it explicitly. More generally, methods in any class are always used with an object of that class, and that object is passed in automatically as the first parameter behind the scenes.
Here is the help for method lower in class str. Notice that you can get help for a single method by prefixing it with the class it belongs to.
| | >>> help(str.lower) |
| | Help on method descriptor lower: |
| | |
| | lower(self, /) unbound builtins.str method |
| | Return a copy of the string converted to lowercase. |
Compare that documentation with the help for the sqrt function in the math module:
| | >>> import math |
| | >>> help(math.sqrt) |
| | Help on built-in function sqrt in module math: |
| | |
| | sqrt(x, /) |
| | Return the square root of x. |
In the help for str.lower, self refers to the string on which the method is being called (the object). The help for math.sqrt doesn’t show any such object.
The general form of a method call is as follows:
| | expression.method_name(arguments) |
So far, every example you’ve seen has a single object as the expression, but any expression can be used as long as it evaluates to the correct type. Here’s an example:
| | >>> ('TTA' + 'G' * 3).count('T') |
| | 2 |
The expression (’TTA’ + ’G’ * 3) evaluates to the DNA sequence ’TTAGGG’, and that is the object that is used in the call on string method str.count.
Here are the steps for executing a method call. These steps are similar to those for executing a function call in .
Evaluate the «expression», which may be something simple, like ’Elizabeth Barrett Browning’ (a poet from the 1800s), or more complicated, like (’TTA’ + ’G’ * 3). Either way, a single object is produced, and that will be the object you are interacting with during the method call.
Now that you have an object, evaluate the method arguments left to right. In our DNA example, the argument is ’T’.
Pass the result of evaluating the initial expression as the first argument, and also pass the argument values from the previous step, into the method.
Execute the method.
When the method call finishes, it produces a value. In the DNA example, ’TTAGGG’.count('T') returns the number of times ’T’ occurs in ’TTAGGG’, which is 2.