12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Putting these two together allows us to call a method of an object. The method callexpression object.method(arguments) is evaluated from left to right—that is, <strong>Python</strong>will first fetch the method of the object, and then call it, passing in the arguments. Ifthe method computes a result, it will come back as the result of the entire methodcallexpression.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, youhave to go through an existing object. Let’s move on to some examples to see how.String Method Examples: Changing StringsTable 7-4 summarizes the call patterns for built-in string methods (be sure to check<strong>Python</strong>’s standard library manual for the most up-to-date list, or run a help call onany string interactively). String methods in this table implement higher-level operationssuch as splitting and joining, case conversions, content tests, and substringsearches.Table 7-4. String method callsS.capitalize( )S.ljust(width)S.center(width) S.lower( )S.count(sub [, start [, end]]) S.lstrip( )S.encode([encoding [,errors]])S.replace(old, new [, maxsplit])S.endswith(suffix [, start [, end]]) S.rfind(sub [,start [,end]])S.expandtabs([tabsize])S.rindex(sub [, start [, end]])S.find(sub [, start [, end]])S.rjust(width)S.index(sub [, start [, end]]) S.rstrip( )S.isalnum( )S.split([sep [,maxsplit]])S.isalpha( )S.splitlines([keepends])S.isdigit( )S.startswith(prefix [, start [, end]])S.islower( ) S.strip( )S.isspace( ) S.swapcase( )S.istitle( ) S.title( )S.isupper( )S.translate(table [, delchars])S.join(seq) S.upper( )Now, let’s work through some code that demonstrates some of the most commonlyused methods in action, and illustrates <strong>Python</strong> text-processing basics along the way.As we’ve seen, because strings are immutable, they cannot be changed in-placedirectly. To make a new text value from an existing string, you construct a new stringwith operations such as slicing and concatenation. For example, to replace two charactersin the middle of a string, you can use code like this:144 | Chapter 7: Strings

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!