6.8. Related Modules

Table 6.9 lists the key related modules for strings that are part of the Python standard library.

Table 6.9. Related Modules for String Types (-Unix only)
ModuleContents
stringstring manipulation and utility functions
reregular expressions: powerful string pattern matching
structconvert strings to/from binary data format
c/StringIOstring buffer object which behaves like a file
cryptperforms one-way encryption cipher
rotorprovides multi-platform en/decryption services

CORE MODULE: string

There are many utility and manipulation functions out there which deal with strings. You may be familiar with some of them if you have programmed in other high-level languages like C, C++, and Java. Python has tried to integrate the most popular functionality into its operators and built-in functions. Nevertheless, they cannot all be integrated into the language. This is where the string module comes in. The string module provides a set of constants as well as module functions that provide additional support for strings.

Some of the key functions in the string module include: ato*()three functions which convert from strings to three numeric types, split()splits up a string into a list of strings, join()does the reverse of split(): merges a list of strings into a single one, and find()searches for substrings.

Refer to the string module documentation for more information and usage of string module attributes. Starting in version 1.6 of Python, many of the functions in the string module have been implemented as string methods, a new feature of strings which begins the journey of obsoleting this module. We introduced you to these methods in Section 6.6.


CORE MODULE: re

Regular expressions (REs) provide advanced pattern matching scheme for strings. Using a separate syntax which describes these patterns, you can effectively use them as “filters” when passing in the text to perform the searches on. These filters allow you to extract the matched patterns as well as perform find-and-replace or divide up strings based on the patterns that you describe.

The re module, introduced in Python 1.5, obsoletes the original regex and regsub modules from earlier releases. It includes a major upgrade in terms of Python's support for regular expressions, adopting the complete Perl syntax for REs. In Python 1.6, the RE engine has been rewritten, for performance improvements as well as support for Unicode strings.

Some of the key functions in the re module include: compile()compiles an RE expression into a reusable RE object, match()attempts to match a pattern from the beginning of a string, search()searches for any matching pattern in the string, and sub()performs a search-and-replace of matches. Some of these functions return match objects with which you can access saved group matches (if any were found). All of Chapter 15 is dedicated to regular expressions.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset