Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's talk about the two different string representations that all Python objects have.
We have a datetime.date
object here that represents the Python 2 end of life date:
>>> from datetime import date
>>> eol = date(2020, 1, 1)
If we type eol
from a Python shell, we're gonna see something that looks kind of like the code that I originally typed in to get that object:
>>> eol
datetime.date(2020, 1, 1)
If we print out this datetime.date
object, we instead see something that looks more like a human-readable date:
>>> print(eol)
2020-01-01
When we called print
, it actually called the built-in str
function to get this string representation:
>>> str(eol)
'2020-01-01'
This is the human-readable string representation for this object.
Likewise, typing just eol
at the Python REPL also resulted in a built-in function being called, the built-in repr
function function:
>>> repr(eol)
'datetime.date(2020, 1, 1)'
This is the programmer-readable string representation for this object.
The human-readable string representation is the thing that an end-user of our program might want to see, which is the reason that it's what we get when we print
something out.
The programmer-readable string representation is what another Python programmer might want to see, which is the reason that we see it when we're playing around with objects at the Python REPL.
If we look at help on the built-in repr
function, we will see that many objects, including most of the built-ins, use a particular convention for the programmer-readable string representation:
>>> help(repr)
Help on built-in function repr in module builtins:
repr(obj, /)
Return the canonical string representation of the object.
For many object types, including most builtins, eval(repr(obj)) == obj.
>>>
This convention says that the string that you get back, should actually be the Python code which if you were to execute it, would give you an object that would be equivalent to the object you started with.
So what that really means is, the result of typing just eol
at the REPL should represent something that if we would execute that code, it would give us back basically the same date
object:
>>> eol
datetime.date(2020, 1, 1)
Now, we should note that most Python objects actually only have one string representation.
The one-string representation they have is the programmer-readable one.
So if we take a list, a dictionary, an integer, a floating-point number, or most objects in Python, and we convert them to a string, we see something that looks like Python code:
>>> numbers = [2, 1, 3, 4, 7]
>>> str(numbers)
'[2, 1, 3, 4, 7]'
If we call the built-in repr
function on them, we'll see the same thing:
>>> repr(numbers)
'[2, 1, 3, 4, 7]'
>>> print(numbers)
[2, 1, 3, 4, 7]
>>> numbers
[2, 1, 3, 4, 7]
Most objects in Python do not have a human-readable string representation. They're meant to be used by other Python programmers, they're not necessarily meant to be printed out.
So in Python, we have two different string representations. str
is the human-readable string representation, if that exists.
If it doesn't exist, the built-in str
function, will call the built-in repr
function, which gives you the programmer-readable string representation, which is meant for other Python programmers.
If you're converting something to a string, the str
function is a pretty great way to do it.
But if you specifically want a string representation of an object that's meant for another Python programmer, you might want to use the built-in repr
function.
To customize these two different string representations on your Python objects, see Customizing the string representation of your Python objects.
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week.
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.