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)
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)
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)
In [8]:
show(ascii, things)
In [9]:
compare(ascii, repr, things)
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]:
In [11]:
a = {'\u03c0': 3.14159}
b = {'π': 3.14159}
a, b, a == b
Out[11]:
In [12]:
a = '\uc548\ub155'
b = '안녕'
a, b, a == b
Out[12]:
In [13]:
a = '\u5b89\u5be7'
b = '安寧'
a, b, a == b
Out[13]:
In [14]:
a = 'D\xe9j\xe0 vu'
b = 'Déjà vu'
a, b, a == b
Out[14]: