ascii versus repr

ascii and repr are similar. They convert arbitrary objects to strings and often return the same string. Both return strings suitable for pasting into source code. ascii returns strings that have only ASCII characters. The difference is provoked for strings which contain non-ASCII characters. repr passes them through as-is, whereas ascii escapes them with ASCII characters.

For example, repr('π') and repr('\u03c0') return 'π' and ascii('π') and ascii('\u03c0') return '\u03c0'.

Here are some objects for which repr and ascii 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],
    """hello""",
    """    """,
    """ """,
    """I'm here.""",
    """Foo said "Hello world!".""",
    """I'm saying "Hello world!".""",
    """\t """,
    """\001""",
    b'a \t\nbyt\'"e string',
    # string with three '
    "'''",
    """'''""",
    # string with three "
    '"""',
    '''"""''',
    # a string with three ' and three "
    """'''"""    '''"""''',
    """'''\"\"\"""",
    # multiline string
    '''sometext
    type some more text
    type something different''',
)

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

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


<built-in function ascii>
0.3333333333333333
0.1
(1+2j)
[1, 'hello']
('creosote', 3)
{2: 'world'}
{'swallow'}
[1.0, {"You're": (3, 'tr"\'e'), 1j: 'complexity'}, 17]
'hello'
'    '
' '
"I'm here."
'Foo said "Hello world!".'
'I\'m saying "Hello world!".'
'\t '
'\x01'
b'a \t\nbyt\'"e string'
"'''"
"'''"
'"""'
'"""'
'\'\'\'"""'
'\'\'\'"""'
'sometext\n    type some more text\n    type something different'
<built-in function repr>
0.3333333333333333
0.1
(1+2j)
[1, 'hello']
('creosote', 3)
{2: 'world'}
{'swallow'}
[1.0, {"You're": (3, 'tr"\'e'), 1j: 'complexity'}, 17]
'hello'
'    '
' '
"I'm here."
'Foo said "Hello world!".'
'I\'m saying "Hello world!".'
'\t '
'\x01'
b'a \t\nbyt\'"e string'
"'''"
"'''"
'"""'
'"""'
'\'\'\'"""'
'\'\'\'"""'
'sometext\n    type some more text\n    type something different'

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(ascii, repr, things)


<built-in function ascii> versus <built-in function repr>
== 0.3333333333333333
== 0.1
== (1+2j)
== [1, 'hello']
== ('creosote', 3)
== {2: 'world'}
== {'swallow'}
== [1.0, {"You're": (3, 'tr"\'e'), 1j: 'complexity'}, 17]
== 'hello'
== '    '
== ' '
== "I'm here."
== 'Foo said "Hello world!".'
== 'I\'m saying "Hello world!".'
== '\t '
== '\x01'
== b'a \t\nbyt\'"e string'
== "'''"
== "'''"
== '"""'
== '"""'
== '\'\'\'"""'
== '\'\'\'"""'
== 'sometext\n    type some more text\n    type something different'

Now to explore where ascii and repr are different.

Strings with non-ASCII characters are used to provoke the difference.


In [6]:
things = (
    'π',
    '\u03c0',
    {'π': 3.14159},
    '안녕',
    '安寧',
    'Déjà vu',
)

In [7]:
show(repr, things)


<built-in function repr>
'π'
'π'
{'π': 3.14159}
'안녕'
'安寧'
'Déjà vu'

In [8]:
show(ascii, things)


<built-in function ascii>
'\u03c0'
'\u03c0'
{'\u03c0': 3.14159}
'\uc548\ub155'
'\u5b89\u5be7'
'D\xe9j\xe0 vu'

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


<built-in function ascii> versus <built-in function repr>
different
    1 8 '\u03c0'
    2 3 'π'
different
    1 8 '\u03c0'
    2 3 'π'
different
    1 19 {'\u03c0': 3.14159}
    2 14 {'π': 3.14159}
different
    1 14 '\uc548\ub155'
    2 4 '안녕'
different
    1 14 '\u5b89\u5be7'
    2 4 '安寧'
different
    1 15 'D\xe9j\xe0 vu'
    2 9 'Déjà vu'

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


In [10]:
a = '\u03c0'
b = 'π'
a, b, a == b


Out[10]:
('π', 'π', True)

In [11]:
a = {'\u03c0': 3.14159}
b = {'π': 3.14159}
a, b, a == b


Out[11]:
({'π': 3.14159}, {'π': 3.14159}, True)

In [12]:
a = '\uc548\ub155'
b = '안녕'
a, b, a == b


Out[12]:
('안녕', '안녕', True)

In [13]:
a = '\u5b89\u5be7'
b = '安寧'
a, b, a == b


Out[13]:
('安寧', '安寧', True)

In [14]:
a = 'D\xe9j\xe0 vu'
b = 'Déjà vu'
a, b, a == b


Out[14]:
('Déjà vu', 'Déjà vu', True)