Computer >> Computer tutorials >  >> Programming >> Python

ascii() in Python program


In this tutorial, we are going to learn about the ascii() function.

ascii(object)

ascii(object) function takes one argument and returns a printable representation of the object.

Let's see some examples.

Example

# intializing non printable characters
string = '¢'
# printing the above character using ascii(object)
print(ascii(string))

Output

If you run the above code, you will get the following results.

'\xa2'

Example

# intializing non printable characters
name = 'HÃFÆËŽ'
# printing the above character using ascii(object)
print(ascii(name))

Output

If you run the above code, you will get the following results.

'H\xc3F\xc6\xcb\u017d'

Example

# intializing non printable characters
strings = ['HÃFÆËŽ', '®', '©Òpy']
# printing the above character using ascii(object)
print(ascii(strings))

Output

If you run the above code, you will get the following results.

['H\xc3F\xc6\xcb\u017d', '\xae', '\xa9\xd2py']

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.