The Wayback Machine - https://web.archive.org/web/20240722104343/https://www.geeksforgeeks.org/ascii-in-python/
Open In App

ascii() in Python

Last Updated : 08 Feb, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In Python, we have ascii() function that returns a string containing a printable representation of an object. In this article, we will see about ascii() in Python.

Python ascii() Function

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.

Python ascii() Function Syntax

Syntax: ascii(object)

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

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

The method can take only one parameter, an object that can be a list, strings, etc. As already discussed, it returns a printable representation of an object.

Usage of Python ascii() Function

We see that in these examples, all the non-ASCII characters have been escaped, i.e. their encoded code gets displayed by using the ascii() method.

Python3




print(ascii("ÂĨ"))


Output

'\xa5'


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, test_str variable contains not-ASCII character and our task is to display its ASCII value from the given string.

Python3




# Python program to illustrate ascii()
test_str = "G ÃŦ Ê k s f ? r G ? e k s"
print(ascii(test_str))


Output

'G \xeb \xea k s f ? r G ? e k s'


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”.

Python3




test_str = '''Geeks
for
geeks'''
print(ascii(test_str))


Output

'Geeks\nfor\ngeeks'


Using Python ascii() on Set

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

Python3




# Python ascii() on Set
test_set = {"Å ", "E", "T"}
print("ascii on Python set:", ascii(test_set))


Output

ascii on Python set: {'\u0160', 'T', 'E'}


Python ascii() on List

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

Python3




# Python ascii() on List
test_list = ["Ň", "ĕ", "Åī"]
print("ascii on Python list:", ascii(test_list))


Output

ascii on Python list: ['\u0147', '\u0115', '\u0174']


Using Python ascii() on Tuple

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

Python3




# Python ascii() on Tuple
test_tuple = ("ÄĒ", "Õ", "Õ", "D")
print("ascii on Python tuple:", ascii(test_tuple))


Output

ascii on Python tuple: ('\u0122', '\xd5', '\xd5', 'D')




Previous Article
Next Article

Similar Reads

What is ASCII - A Complete Guide to Generating ASCII Code
The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating back to its development in the early 1960s. Origi
13 min read
Substring with maximum ASCII sum when some ASCII values are redefined
Given a string W, and two arrays X[] and B[] of size N each where the ASCII value of character X[i] is redefined to B[i]. Find the substring with the maximum sum of the ASCII (American Standard Code for Information Interchange) value of the characters. Note: Uppercase & lowercase both will be present in the string W. Input: W = "abcde", N = 1,
9 min read
Check if a string contains a non-ASCII value in Julia - ascii() Method
The ascii() is an inbuilt function in Julia which is used to convert a specified string to a String type and also check the presence of ASCII data. If the non-ASCII byte is present, throw an ArgumentError indicating the position of the first non-ASCII byte. Syntax: ascii(s::AbstractString) Parameters:s::AbstractString: Specified stringReturns: It r
1 min read
ASCII NULL, ASCII 0 ('0') and Numeric literal 0
The ASCII (American Standard Code for Information Interchange) NULL and zero are represented as 0x00 and 0x30 respectively. An ASCII NULL character serves as a sentinel character of strings in C/C++. When the programmer uses '0' in his code, it will be represented as 0x30 in hex form. What will be filled in the binary integer representation in the
3 min read
Python | ASCII art using pyfiglet module
pyfiglet takes ASCII text and renders it in ASCII art fonts. figlet_format method convert ASCII text into ASCII art fonts. It takes following arguments : text font ( DEFAULT_FONT = 'standard' ) Command to install pyfiglet module : pip install pyfiglet Code #1: Text in default font # import pyfiglet module import pyfiglet result = pyfiglet.figlet_fo
6 min read
Converting an Integer to Ascii Characters in Python
In Python, working with integers and characters is a common task, and there are various methods to convert an integer to ASCII characters. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers. In this article, we will explore some simple and commonly used methods for convertin
3 min read
Python Encode Unicode and non-ASCII characters into JSON
This article will provide a comprehensive guide on how to work with Unicode and non-ASCII characters in Python when generating and parsing JSON data. We will look at the different ways to handle Unicode and non-ASCII characters in JSON. By the end of this article, you should have a good understanding of how to work with Unicode and non-ASCII charac
5 min read
How To Fix - UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128) in Python
Several errors can arise when an attempt to change from one datatype to another is made. The reason is the inability of some datatype to get casted/converted into others. One of the most common errors during these conversions is Unicode Encode Error which occurs when a text containing a Unicode literal is attempted to be encoded bytes. This article
2 min read
Convert Unicode to ASCII in Python
Unicode is the universal character set and a standard to support all the world's languages. It contains 140,000+ characters used by 150+ scripts along with various symbols. ASCII on the other hand is a subset of Unicode and the most compatible character set, consisting of 128 letters made of English letters, digits, and punctuation, with the remain
2 min read
Transliterating non-ASCII characters with Python
Transliteration is a process of writing the word of one language using similarly pronounced alphabets in other languages. It deals with the pronunciation of words in other languages. Similarly, in computer language, the computer can handle ASCII characters but has problems with non-ASCII characters. There are some times when we are unable to skip n
3 min read
three90RightbarBannerImg