Sub topic 4
print() versus the echoed repr
This chapter assumes you can start the shell and type expressions at the >>> prompt. You should also know basic Python types such as strings, lists, and classes.
Two ways to show a value
The shell can show a value in two ways, and they often look different. When you type an expression, the shell echoes its repr. When you call print(), Python writes the value's str.
repr(x)gives a string meant for developers. It tries to show exactly what the value is, ideally as code you could type back in.str(x)gives a string meant for readers. It is the friendly form.
The shell's echo calls repr() on the result. print() calls str() and writes the text, followed by a newline.
Seeing the difference with strings
Strings show the difference most clearly.
>>> s = "hello\n"
>>> s
'hello\n'
>>> print(s)
hello
>>>The echo shows quotes and the escape sequence \n, so you can see that the string ends in a newline. print(s) writes the characters themselves. The newline in s moves the cursor down, and print() adds a second newline. That produces the blank line.
The same applies to numbers and the strings that look like them.
>>> 42
42
>>> "42"
'42'
>>> print(42)
42
>>> print("42")
42The echo tells 42 and '42' apart. print() shows the same text for both. When a result looks wrong, echo it instead of printing it.
Calling repr() and str() directly
You can call both functions yourself.
>>> str("hi")
'hi'
>>> repr("hi")
"'hi'"
>>> print(repr("hi"))
'hi'repr("hi") returns a string that contains the quote characters. The shell then echoes that string, and it picks double quotes to show a string that holds single quotes. print(repr(...)) is a quick way to see the repr as plain text.
Defining your own repr and str
A class controls both forms through the special methods __repr__ and __str__.
>>> class Point:
... def __init__(self, x, y):
... self.x = x
... self.y = y
... def __repr__(self):
... return f"Point({self.x}, {self.y})"
... def __str__(self):
... return f"({self.x}, {self.y})"
...
>>> p = Point(1, 2)
>>> p
Point(1, 2)
>>> print(p)
(1, 2)
>>> [p]
[Point(1, 2)]
>>> print([p])
[Point(1, 2)]The last two lines show a common surprise. Containers such as lists use the repr of their items, even when you print them. If a class defines only __repr__, str() falls back to it. If a class defines neither, you get a default such as <__main__.Point object at 0x...>, and the address varies.
Common errors
- Reading a missing newline as a bug.
'hello'andhelloare the same text. The quotes belong to the echo, not to the string. - Expecting print() to return its text.
print()returnsNone. If you writeresult = print("hi"),resultisNone, and the shell echoes nothing for it. - Debugging with print() alone. Trailing spaces, tabs, and
\rcharacters are invisible in printed output. Echo the value, or useprint(repr(value)), to see them. - Relying on the echo inside a script. The automatic echo happens only in the interactive shell. In a file, a bare expression produces no output, so you must call
print().
Exercise
In the shell, what is the difference between typing "a\tb" and typing print("a\tb")?
Show answer
Typing "a\tb" echoes the repr, which is 'a\tb' with quotes and a visible backslash-t. Typing print("a\tb") writes the characters a, a real tab, and b, with no quotes, then a newline.