Remember
ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.
ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'café'
print(ascii(text))Top-level scope
'café'ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'café'
print(ascii(text))
|
v
DISPLAYED RESULT
'caf\xe9'
FINAL BINDINGS
text --> object 1 (str)
object 1: 'café'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'café'
print(ascii(text))Top-level scope
'café'The example has finished. Compare the final output with the values and references above.
'caf\xe9'
What do you expect this program to print?
text = 'café'
print(ascii(text))ascii() FunctionThe ascii() function returns a string that contains a printable representation of an object, but escapes non-ASCII characters using \x, \u, or \U escapes.
It is especially useful when you want to view or debug strings that contain non-English or special characters.
ascii(object)
object – Any Python object (commonly a string or list)string that contains the ASCII-only representation of the objecttext = "café"
print(ascii(text))
'caf\xe9'
Here, the accented character é is escaped as \xe9.
print(ascii("Python 🐍"))
'Python \U0001f40d'
names = ["José", "Zoë", "Müller"]
print(ascii(names))
['Jos\xe9', 'Zo\xeb', 'M\xfcller']
The repr() function returns a string representation, but keeps non-ASCII characters as-is if possible. In contrast, ascii() always escapes them.
print(repr("café")) # 'café'
print(ascii("café")) # 'caf\xe9'
ascii() with ord() – they are differentascii() does not encode or convert characters; it only escapes them for safe viewingKnow the difference between str(), repr(), and ascii(). Interviewers often ask about string representation functions and how they behave with special characters.
ascii() returns a printable representation of an objectTry printing ASCII-safe versions of the following:
words = ["niño", "français", "中文"]
for word in words:
print(ascii(word))
'ni\xf1o'
'fran\xe7ais'
'\u4e2d\u6587'
Connect what you noticed in the example with the idea below.
ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.
ascii() deletes every non-ASCII character.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.