These topics are commonly overlooked by new Python programmers.
Let's talk about the two different string representations that all Python objects have.
>>> 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
>>> str(eol) '2020-01-01'
This is the human-readable string representation for this object.
>>> 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
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
>>> 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
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.