repr versus str

str and repr are similar. They convert arbitrary objects to strings and often return the same string. Here are some objects for which repr and str return exactly the same strings.


In [1]:
things = (
    1/3,
    .1,
    1+2j,
    [1, 'hello'],
    ('creosote', 3),
    {2: 'world'},
    {'swallow'},
    [1., {"You're": (3, '''tr"'e'''), 1j: 'complexity'}, 17],
)

In [2]:
def show(function, things):
    print(function)
    for thing in things:
        print(function(thing))

In [3]:
show(str, things)
show(repr, things)


<class 'str'>
0.3333333333333333
0.1
(1+2j)
[1, 'hello']
('creosote', 3)
{2: 'world'}
{'swallow'}
[1.0, {1j: 'complexity', "You're": (3, 'tr"\'e')}, 17]
<built-in function repr>
0.3333333333333333
0.1
(1+2j)
[1, 'hello']
('creosote', 3)
{2: 'world'}
{'swallow'}
[1.0, {1j: 'complexity', "You're": (3, 'tr"\'e')}, 17]

In [4]:
def compare(f1, f2, things):
    print(f1, 'versus', f2)
    for thing in things:
        t1 = f1(thing)
        t2 = f2(thing)
        if t1 == t2:
            print('==', t1)
        else:
            print('different')
            print('    1', len(t1), t1)
            print('    2', len(t2), t2)

In [5]:
compare(str, repr, things)


<class 'str'> versus <built-in function repr>
== 0.3333333333333333
== 0.1
== (1+2j)
== [1, 'hello']
== ('creosote', 3)
== {2: 'world'}
== {'swallow'}
== [1.0, {1j: 'complexity', "You're": (3, 'tr"\'e')}, 17]

Now to explore where repr and str are different.

For strings, str just returns the unmodified input whereas repr makes a string suitable for pasting into source code, including correct quoting, even for strings with quoting characters within them. repr also makes invisible stuff of strings visible.


In [6]:
things = (
    """hello""",
    """    """,
    """ """,
    """I'm here.""",
    """Foo said "Hello world!".""",
    """I'm saying "Hello world!".""",
    """\t """,
    """\001""",
    """π""",
)

In [7]:
show(str, things)


<class 'str'>
hello
    
 
I'm here.
Foo said "Hello world!".
I'm saying "Hello world!".
	 

π

In [8]:
show(repr, things)


<built-in function repr>
'hello'
'    '
' '
"I'm here."
'Foo said "Hello world!".'
'I\'m saying "Hello world!".'
'\t '
'\x01'
'π'

In [9]:
compare(str, repr, things)


<class 'str'> versus <built-in function repr>
different
    1 5 hello
    2 7 'hello'
different
    1 4     
    2 6 '    '
different
    1 1  
    2 3 ' '
different
    1 9 I'm here.
    2 11 "I'm here."
different
    1 24 Foo said "Hello world!".
    2 26 'Foo said "Hello world!".'
different
    1 26 I'm saying "Hello world!".
    2 29 'I\'m saying "Hello world!".'
different
    1 2 	 
    2 5 '\t '
different
    1 1 
    2 6 '\x01'
different
    1 1 π
    2 3 'π'

For basic data types, repr makes a string suitable for pasting into source code.


In [10]:
s = 'I\'m saying "Hello world!".'
print(s)


I'm saying "Hello world!".

In [11]:
print(str(s))
print(repr(s))


I'm saying "Hello world!".
'I\'m saying "Hello world!".'

I often use repr for debugging output to see details.

That is my main use of repr.


Extra stuff

The following arose during the presentation of the above.

Someone asked how repr handles bytestrings compared to str, so we played with that. It turns out that they are identical.


In [12]:
things = (
    b'a \t\nbyt\'"e string',
)

In [13]:
compare(str, repr, things)


<class 'str'> versus <built-in function repr>
== b'a \t\nbyt\'"e string'

Quoting strings with ''' or """ and how to encode ''' or """ in a string.


In [14]:
things = (
    # string with three '
    "'''",
    """'''""",
    # string with three "
    '"""',
    '''"""''',
    # a string with three ' and three "
    """'''"""    '''"""''',
    """'''\"\"\"""",
    # multiline string
    '''sometext
    type some more text
    type something different''',
)

In [15]:
compare(str, repr, things)


<class 'str'> versus <built-in function repr>
different
    1 3 '''
    2 5 "'''"
different
    1 3 '''
    2 5 "'''"
different
    1 3 """
    2 5 '"""'
different
    1 3 """
    2 5 '"""'
different
    1 6 '''"""
    2 11 '\'\'\'"""'
different
    1 6 '''"""
    2 11 '\'\'\'"""'
different
    1 61 sometext
    type some more text
    type something different
    2 65 'sometext\n    type some more text\n    type something different'