Python ascii()

Python ascii() takes any object as input and returns a string where all non-ASCII characters are converted into their corresponding \x, \u, or \U escape codes, making the output safe to use in contexts where only ASCII is expected.
Table of Contents

Understanding Python ascii() Function

Python ascii() is a built-in function that takes an object as input and returns a printable representation of that object as a string. But, any non-ASCII characters in the string are escaped using \x, \u, or \U escapes. Basically, it converts any object into a string where all the non-ascii characters are replaced with their escape sequences, making it safe to display or store in contexts where only ASCII characters are allowed. It is particularly useful when dealing with text data containing special characters, Unicode characters, or other non-printable characters.

Syntax of Python ascii()

string_representation = ascii(object)

Explanation

  • string_representation: Variable stores the printable ASCII string representation of the object.
  • ascii(): Built-in function that generates the ASCII representation.
  • object: Object (string, list, tuple, etc.) you want to convert. It is used as input to the ascii() function.

Example of Python ascii()

text = "Héllö Wörld"
ascii_text = ascii(text)
print(ascii_text)

Explanation

  • text = "Héllö Wörld": Assigns a string with non-ASCII characters to the variable text.
  • ascii_text = ascii(text): The ascii() function converts text to its ASCII representation and stores it in ascii_text.
  • print(ascii_text): Prints the value of ascii_text, which is ‘H\xe9ll\xf6 W\xf6rld’.

Output

‘H\xe9ll\xf6 W\xf6rld’

Example 2

text = "Café"  # Contains a non-ASCII character (é)
ascii_text = ascii(text)
print(ascii_text)

Explanation

  • text = "Café": Assigns a string with a non-ASCII character to the text variable
  • ascii_text = ascii(text): The ascii() function converts the non-ASCII character ‘é’ into its escape sequence ‘\xe9’
  • print(ascii_text): Prints the ASCII-safe representation

Output

‘Caf\xe9’


Python ascii() with List

When you use Python ascii() with a list, it returns a string representation of the list where any non-ASCII characters within the list’s elements (if those elements are strings) are replaced with their corresponding escape sequences. This is useful for debugging or displaying lists containing non-printable or non-ASCII characters. It provides a way to visualize the list’s contents safely and consistently.

Example

my_list = ["apple", "bànànà", "cherry"]
ascii_list = ascii(my_list)
print(ascii_list)

Explanation

  • my_list = ["apple", "bànànà", "cherry"]: Creates a list named my_list containing strings, one with non-ASCII characters.
  • ascii_list = ascii(my_list): The ascii() function converts my_list to its ASCII representation and stores it in ascii_list.
  • print(ascii_list): Prints the value of ascii_list, which is ['apple', 'b\\xe0n\\xe0n\\xe0', 'cherry'].

Output

[‘apple’, ‘b\xe0n\xe0n\xe0’, ‘cherry’]


Python ascii() with Set

Applying the Python ascii() function to a set results in a string representation of the set. Like lists, any non-ASCII characters within the set’s elements (if the elements are strings) are replaced with their corresponding escape sequences. This can be helpful when you need to display or serialize a set in a way that is guaranteed to be safe for ASCII-only environments. It ensures that the set’s contents are represented using printable ASCII characters.

Example

my_set = {"apple", "bànànà", "cherry"}
ascii_set = ascii(my_set)
print(ascii_set)

Explanation

  • my_set = {"apple", "bànànà", "cherry"}: Creates a set named my_set containing strings, one with non-ASCII characters.
  • ascii_set = ascii(my_set): The ascii() function converts my_set to its ASCII representation and stores it in ascii_set.
  • print(ascii_set): Prints the value of ascii_set, which will be a string representation of the set with escaped characters (order may vary as sets are unordered).

Output

{‘apple’, ‘b\xe0n\xe0n\xe0’, ‘cherry’}


Python ascii() with Tuple

When Python ascii() is used with a tuple, it generates a string representation of the tuple, replacing any non-ASCII characters within the tuple’s elements with their corresponding escape sequences. This ensures that the tuple’s contents are represented using only printable ASCII characters. It is particularly useful when displaying or serializing a tuple safely for environments that may not support non-ASCII characters.

Example

my_tuple = ("apple", "bànànà", "cherry")
ascii_tuple = ascii(my_tuple)
print(ascii_tuple)

Explanation

  • my_tuple = ("apple", "bànànà", "cherry"): Creates a tuple named my_tuple containing strings, one with non-ASCII characters.
  • ascii_tuple = ascii(my_tuple): The ascii() function converts my_tuple to its ASCII representation and stores it in ascii_tuple.
  • print(ascii_tuple): Prints the value of ascii_tuple, which is ('apple', 'b\\xe0n\\xe0n\\xe0', 'cherry').

Output

{‘apple’, ‘b\xe0n\xe0n\xe0’, ‘cherry’}


Conclusion

Python ascii() function is a valuable tool for working with text data that may contain non-ASCII characters. It provides a way to obtain a printable string representation of various Python objects, including strings, lists, sets, and tuples, by replacing non-ASCII characters with their escape sequences. This ensures the output is safe for display or storage in environments that only support ASCII characters. Understanding how ascii() works can be helpful for debugging, data serialization, and handling text consistently and reliably.


Also Read

Python all()

Python bin()


Python Reference

python ascii()

Table of Contents