Python's built-in classes support a variety of representations of data, together with operations that are suitable for those representations. The built-in classes includes numerical types as well as those implementing containers or collections, that is, objects that can hold groups of other objects. Before diving into some of this, let's first examine more how to get information about different classes (whether built-in or custom defined).

To retrieve information on a class, we use the help() function. For example, here is what is returned by help(str):

Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |
 |  Methods defined here:       [...long list...]
Python

After hitting the spacebar a few times to scroll down the list, we start seeing the names of useful-looking methods such as upper and lower. Just as the help documentation indicates, the way to invoke methodname on a string object strg_obj is to enter strg_obj.methodname(arguments), like this:

>>> strg_obj = 'Apple Orange Banana I love them all'
>>> strg_obj.upper()
'APPLE ORANGE BANANA I LOVE THEM ALL'
>>> strg_obj.lower()
'apple orange banana i love them all'
>>> strg_obj.count('apple')
0
>>> strg_obj.count('Apple')
1
Python

This kind of "dot" syntax for invoking a method on an object is familiar from OOP languages such as C++ and Java. In Python, the dot operator ( . ) is used for accessing attributes in the namespace of an object (both data attributes and methods, which are functions attached to objects).

You can also get a list of the contents (i.e., all the names in the namespace) of class str:

>>> dir(str)
['__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', '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']
Python

For help on a specific content element, say the upper method of the str class:

>>> help(str.upper)
Help on method_descriptor:

upper(...)
    S.upper() -> str

    Return a copy of S converted to uppercase.
Python

Note that, as per the help, S.lower() and S.upper() return a string resulting from the specified operation being performed on S. But S, the string itself, has not changed. Strings are immutable sequence objects and do not have methods that change the object itself. This is not necessarily the case for mutable objects such as lists.

 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)