Your Guide to the Python Standard Library - dummies

Your Guide to the Python Standard Library

By Alan Shovic, John Shovic

The Python standard library is basically all the stuff you get when you get the Python languages. That includes all the Python data types like string, integer, float, and Boolean. Every instance of those data types is actually an instance of a class defined in the Python standard library.

For this reason, the terms type, instance, and object are often used interchangeably. An integer is a whole number; it’s also a data type in Python. But it exists because the standard library contains a class for integers, and every integer you create is actually an instance of that class and hence an object (because classes are the templates for things called objects).

The type() function in Python usually identifies the type of a piece of data. For example, run these two lines of code at a Python prompt, in a Jupyter notebook or a .py file:

x = 3
print(type(x))

The output is:

<class 'int'>

This is telling you that x is an integer, and also that it’s an instance of the int class from the standard library. Running this code:

x = 'howdy'
print(type(x))

Produces this output:

<class 'str'>

That is, x contains data that’s the string data type, created by the Python str class. The same thing works for a float (a numeric value with a decimal point, like 3.14) and for Booleans (True or False).

Python’s dir() function

The Python standard library offers a dir() method that displays a list of all the attributes associated with a type. For example, in the previous example the result <class ‘str’> tells you that the data is the str data type. So you know that’s a type, and thus in instance of a class called str (short for string). Entering this command:

dir(str)

Displays something like this:

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__','__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

The dunder named items (the names surrounded by double-underlines) usually represent something that’s built into Python and that plays some role in the Python language that you don’t necessarily access directly. These are often referred to as special variables or magic methods. For example, there’s an __add__ method that’s actually invoked by using the + (addition) operator to add two numbers or join together two strings.

The regular functions don’t have the double underscores and are typically followed by parentheses. For example, take a look at these lines of code:

x = "Howdy"
print(type(x), x.isalpha(), x.upper())

The output from that code is:

<class 'str'> True HOWDY

The first part, <class 'str'> tells you that x contains a string. As such, you can use any of the attributes shown in the output of dir(str) on it. For example, the True is the output from x.isalpha() because x does contain alphabetic characters. The HOWDY is the output of x.upper(), which converts the string to all uppercase letters.

Beginners often wonder what good seeing a bunch of names like 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', and so forth does for you when you don’t know what the names mean or how to use them. Well, they don’t really help you much if you don’t pursue it any further. You can get some more detailed information by using help() rather than dir.

Python’s help() function

The Python prompt also offers a help() function with the syntax:

help(object)

To use it, replace object with the object type with which you’re seeking help. For example, to get help with str objects (strings, which come from the str class) enter this command at the Python prompt:

help(str)

The output will be more substantial information about the topic in the parentheses. For example, where dir(str) lists the names of attributes of that type, help(dir) provides more detail about each item. For example, whereas dir(str) tells you that there’s a thing called capitalize in the str class, help tells you a bit more about it, as follows:

capitalize(self, /)
Return a capitalized version of the string.
More specifically, make the first character have upper case and the rest lower case.

The word self there just means that whatever word you pass to capitalize is what gets capitalized. The / at the end marks the end of positional-only parameters, meaning that you can’t use keywords with parameters after that like you can when defining your own functions.

What usually works best for most people is a more in-depth explanation and one or more examples. For those, Google or a similar search engine is usually your best bet. Start the search with the word Python (so it knows what the search is in reference too) followed by the exact word with which your seeking assistance. For example, searching Google for

python capitalize

… provides links to lots of different resources for learning about the capitalize attribute of the str object, including examples of its use.

If you get tired of pressing any key to get past More … at the end of every page in help, just press Ctrl+C. This gets you back to the Python prompt.

Of course, a really good (albeit technical) resource for the Python standard library is the standard library documentation itself. This is always available at usually under the link Library Reference. But even that wording may change, so if in doubt, just google python standard library. Just be forewarned that it is huge and very technical. So don’t expect to memorize or even understand it all right off the bat. Use it as an ongoing resource to learn about things that interest you as your knowledge of Python develops.

The documentation that appears at docs.python.org will generally be for the current stable version. Links to older versions, and to any newer versions that may be in the works when you visit, are available from links at the left side of the page.

Exploring Python built-in functions

Both dir() and help() are examples of Python built-in functions. These are functions that are always available to you in Python, in any app you’re creating as well as at the Python command prompt. These built-in functions are also a part of the standard library

In fact, if you google Python built-in functions, some of the search results will point directly to the Python documentation. Clicking that link will open that section of the standard library documentation and displays a table of all the built-in functions. On that page, you can click the name of any function to learn more about it.

Python's built-in functions
Python’s built-in functions.