![]() |
VOOZH | about |
Python provides the built-in ascii() function to return a printable representation of an object using only ASCII characters. Any non-ASCII characters present in the object are automatically escaped using Unicode escape sequences such as \x, \u, or \U.
This function accepts a single argument and returns a string, making it particularly useful in scenarios where Unicode characters may not be supported, such as logging, debugging, or working in ASCII-restricted environments.
'\xa5'
Explanation: The yen symbol (¥) is a non-ASCII character, so ascii() replaces it with its Unicode escape sequence \xa5, ensuring output contains only ASCII characters.
The ascii() function can be applied to various Python objects, including:
All non-ASCII characters present in the string are replaced with their corresponding Unicode escape sequences, while ASCII characters remain unchanged.
'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.
The newline characters are represented using the escape sequence \n. The output remains ASCII-safe while preserving the structure of the original string.
'Geeks\nfor\ngeeks'
Explanation: The code defines a multi-line string susing 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).
The non-ASCII character in the set is converted into its Unicode escape sequence, while ASCII characters remain unchanged.
{'\u0160', 'E', 'T'}
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.
Each element in the list is processed individually and all non-ASCII characters are converted into Unicode escape sequences.
['\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.
The tuple elements containing non-ASCII characters are replaced with their Unicode escape equivalents, while ASCII characters are left unchanged.
('\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.
Both repr() and ascii() return string representations of Python objects, but they differ in handling non-ASCII characters. repr() keeps Unicode characters intact, while ascii() escapes them to ensure ASCII-only output.
| Feature | repr() | ascii() |
|---|---|---|
| Purpose | Returns a string representation of an object | Returns an ASCII-safe string representation |
| Handling of Non-ASCII Characters | Displays Unicode characters as they are | Escapes non-ASCII characters using Unicode sequences |
Character Conversion | No character escaping is performed | Converts characters to \x, \u, or \U format |
| Typical Use Case | Debugging and logging with full Unicode support | Output in systems or logs that require ASCII-only text |
| Example Output | repr('café') -> 'café' | ascii('café') -> 'caf\\u00e9' |