5.4. Modules

Python programs can consists of many files. Each file is known as a module. To gain access to the contents of a Python module, import the module by using the import keyword.

Python comes with a vast array of modules which, by default, live in the python/lib subdirectory.

The following example imports the string module and calls the upper function it provides. This function converts a string to all uppercase characters.

CD-ROM reference=5024.txt
>>> import string
>>> print string.upper ("Hello World")
HELLO WORLD
>>>

Python is case sensitive. This case sensitivity applies to keywords, variables, and modules.

The following statement fails because Import is not a keyword.

CD-ROM reference=5025.txt
>>> Import string Syntax Error: ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.