Last Updated : 26 Apr, 2025
Python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. It's a built-in function that takes one argument and returns a string that represents the object using only ASCII characters. Example:
Python
Explanation: The code prints the ASCII-safe representation of the yen symbol (¥) as its Unicode escape sequence '\xa5'.
Syntaxascii(object)
Parameters:
Return Type: Returns a string as a printable representation of the object passed, escaping the non-ASCII characters.
Usage of Python ascii() FunctionWe can use Python ascii() function in the following ways:
In this example, s variable contains not-ASCII character and our task is to display its ASCII value from the given string.
Python
s = "G ë ê k s f ? r G ? e k s"
print(ascii(s))
'G \xeb \xea k s f ? r G ? e k s'
Explanation: The code defines a string s with special characters and spaces. ascii(s) returns a string where all non-ASCII characters are replaced with their Unicode escape sequences.
Python ascii() on new line charactersHere we take a variable with multiline string and pass it into the ascii() and it returns "\n", the value of new line is "\n".
Python
s = '''Geeks
for
geeks'''
print(ascii(s))
'Geeks\nfor\ngeeks'
Explanation: The code defines a multi-line string s using triple quotes ('''). ascii(s) converts any non-ASCII characters in the string to their Unicode escape sequences, and it also handles special characters like newline (\n).
Using Python ascii() on SetBelow example shows how to use Python ascii() on Python Set.
Python
s = {"Š", "E", "T"}
print(ascii(s))
{'\u0160', 'T', 'E'}
Explanation: The code defines a set s containing the characters "Š", "E", and "T". ascii(s) returns a string representation of the set, where any non-ASCII characters are replaced with their Unicode escape sequences.
Python ascii() on ListIn this example, we are using ascii() on Python List.
Python
a = ["Ň", "ĕ", "Ŵ"]
print(ascii(a))
['\u0147', '\u0115', '\u0174']
Explanation: The code defines a list a containing the characters "Ň", "ĕ", and "Ŵ". ascii(a) converts any non-ASCII characters in the list to their Unicode escape sequences.
Using Python ascii() on TupleIn this example, we are using Python ascii() on Python Tuple.
Python
t = ("Ģ", "Õ", "Õ", "D")
print(ascii(t))
('\u0122', '\xd5', '\xd5', 'D')
Explanation: The code defines a tuple t containing the characters "Ģ", "Õ", "Õ", and "D". ascii(t) converts any non-ASCII characters in the tuple to their Unicode escape sequences.
Differences Between repr() and ascii() Feature repr() ascii() Purpose Returns a string representation of an object Returns a string representation with non-ASCII characters escaped Non-ASCII Handling Keeps non-ASCII characters as-is Converts non-ASCII characters into Unicode escape sequences (\uXXXX
) Use Case Useful for debugging and logging where full data (including Unicode) is needed Useful for output in environments that may not support non-ASCII characters Output Example repr('café') → 'café' ascii('café') → 'caf\\u00e9'
Related Article:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4