HomePython Page 8 - Python 101 (part 7): Dinner With A Hungry Giant
String Theory (And Other Interesting Stuff) - Python
Python allows developers to logically group functions togetherinto modules, which can be imported and used by any Python program. In thisarticle, find out what a module is, learn how modules and module namespaceswork, and check out the default modules that ship with Python
Python comes with a whole bunch of built-in modules, which can substantially reduce the time you spend on development. Here's a list of the most common and useful ones (some of these are only available in Python 2.x):
The "string" module handles common string operations,
>>> import string
>>> string.lower("HELLO")
'hello'
>>> string.center("HELLO", 80)
' HELLO
'
>>> string.split("The red wolf ate the green pumpkin", " ")
['The', 'red', 'wolf', 'ate', 'the', 'green', 'pumpkin']
>>>
while the "re" module matches regular expression via its match() and search() functions,
>>> import re
>>> re.search("at", "Batman - Dark Knight")
>>> re.findall("oo", "boom boom bang")
['oo', 'oo']
>>>
and the "difflib" and "filecmp" modules help in comparing strings, files and directories.
The "math" module does for numbers what the "string" module does for strings.
The "cgi", "urllib" and "httplib" modules are used to connect your Python program to the Web; the "smtplib" and "poplib" modules help in writing mail clients; the "mimetools" module helps to process MIME-encapsulated email messages; and the "xmllib", "xml.dom" and "xml.sax" modules provide the architecture necessary to handle XML data. Whew!
In case you need to find out more about a specific module - or any other Python object - consider using the dir() function, which returns a list of object attributes.