Open In App

ascii() in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
print(ascii("¥"))

Output
'\xa5'

Explanation: The code prints the ASCII-safe representation of the yen symbol (¥) as its Unicode escape sequence ‘\xa5’.

Syntax

ascii(object)

Parameters:

  • object: Any Python object (ex.: string, int, list, tuple, set, etc.)

Return Type: Returns a string as a printable representation of the object passed, escaping the non-ASCII characters.

Usage of Python ascii() Function

We can use Python ascii() function in the following ways:

  • Using ascii() on Python String containing non-ASCII characters
  • Python ascii() on new line characters
  • Using Python ascii() on Set
  • Using Python ascii() on Tuple
  • Using Python ascii() on List

Using ascii() on Python String containing non-ASCII characters

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))

Output
'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 characters

Here 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))

Output
'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 Set

Below example shows how to use Python ascii() on Python Set.

Python
s = {"Š", "E", "T"}
print(ascii(s))

Output
{'\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 List

In this example, we are using ascii() on Python List.

Python
a = ["Ň", "ĕ", "Ŵ"]
print(ascii(a))

Output
['\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 Tuple

In this example, we are using Python ascii() on Python Tuple.

Python
t = ("Ģ", "Õ", "Õ", "D")
print(ascii(t))

Output
('\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()

Featurerepr()ascii()
PurposeReturns a string representation of an objectReturns a string representation with non-ASCII characters escaped
Non-ASCII HandlingKeeps non-ASCII characters as-isConverts non-ASCII characters into Unicode escape sequences (\uXXXX)
Use CaseUseful for debugging and logging where full data (including Unicode) is neededUseful for output in environments that may not support non-ASCII characters
Output Examplerepr(‘café’) → ‘café’ascii(‘café’) → ‘caf\\u00e9’

Related Article:



Next Article

Similar Reads