Next Previous Table of Contents

Opening the Box

So far, we know that there are a few different data types in Python. However, we've only scratched the surface of what these types.

Everything in Python is an object. What does that mean? Everything in Python is a container, and one that we can, and often should, peek inside.

When we look inside an object in Python, we will find one of two things that should now be familiar to us: data and functions. Typically, if there is data, it will tell us something about the object. If there is a function, it will act on the object, or otherwise do something semantically connected with the object.

Let's start opening these mysterious boxes and see what's inside! First, create a string and assign it. You can use any bit of text you like, but I'll use the first line from a favorite short story by Ursula Le Guin.

>>> omelas = "With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea."

Notice there is no output. Think of the output as going into the variable. If we want Python to show us the data represented by the variable name, we can enter the name by itself.

>>> omelas
'With a clamor of bells that set the swallows soaring, the Festival of Summer came to the city Omelas, bright-towered by the sea.'

For fun and for practice, let's use the type() function to confirm that the name omelas is assigned to a string.

>>> type(omelas)
<class 'str'>

Now, let's try out a new function, the dir() function. Before trying it, I warn you that the output can be a little intimidating. I'm going to work with you to understand it, so don't get too stressed out!

>>> dir(omelas)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', ... 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', '... 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

I've added some ellipses, and the full output should be larger.

Yikes! What is all this? Well, every string gives us a huge number of options for ways to query, cut, transform, replace, and otherwise play with the text. When looking at the options, you will likely want to ignore the items that have underscores around them—let's just not worry about those for now.

Programmers use special names for functions and data that live inside objects. They're called "methods" and "attributes," respectively. (It's a pain to learn this new and kind of redundant vocabulary so early, but these words are used a lot by programmers and so it's good to get used to it.)

Most of these items are methods (object functions) that operate on the string itself. For example, we can make our first line all uppercase:

>>> omelas.upper()

Notice that the syntax for accessing methods (functions inside objects) and attributes (data inside objects) is to use a dot or period (.). This is a syntax you'll use frequently in Python to access things that are inside other things.

Take awhile to explore the methods inside our omelas string. The ones of most interest are below:


You can either just try them out, or use a search engine to look up what they do. To search, type something like " string method python."

Next Previous Table of Contents