Python ascii() Built in Function
The ascii() function in Python returns a readable version of any object (Strings, Tuples, Lists, etc). The ascii() function will replace any non-ascii characters with escape characters. ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard that uses numbers from 0 to 127 to represent English characters. For example, ASCII code for the character A is 65, and 90 is for Z. Similarly, ASCII code 97 is for a, and 122 is for z.
Here are some examples of Python code using ascii():
# Print ascii representation of a string
normal_text = 'Python is interesting'
print(ascii(normal_text)) # prints 'Python is interesting'
# Print ascii representation of a string with non-ascii characters
other_text = 'Pythön is interesting'
print(ascii(other_text)) # prints 'Pyth\\xf6n is interesting'
# Print ascii representation of a list
my_list = ['a', 'b', 'c', '√']
print(ascii(my_list)) # prints "['a', 'b', 'c', '\\u221a']"
# Print ascii representation of a tuple
my_tuple = ('x', 'y', 'z', 'Ø')
print(ascii(my_tuple)) # prints "('x', 'y', 'z', '\\xd8')"
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment