ascii(): string representation of an object, converting any non-ASCII characters into escape sequences

ascii() to return printable chars

Example : ( with output after comment # )
print(ascii('Ù')) # '\xd9'
print(ascii('å')) # '\xe5'
print(ascii('õ')) # '\xf5'
print(ascii('÷')) # '\xf7'
Using string
print(ascii('þlûs2ñet'))
Output
'\xfel\xfbs2\xf1et'
Using List
my_list=['Oñe','Twö','Threë'] 
print(ascii(my_list))
Output
['O\xf1e', 'Tw\xf6', 'Thre\xeb']
Using for loop to display Unicode point value with equivalent char and printable char
for i in range(200,250): 
    print(i,":",chr(i),ascii(chr(i)))

Example 1: Using ascii() with Non-ASCII Characters

print(ascii('Pythön is fun')) # 'Pyth\xf6n is fun'

Example 2: ascii() with Lists Containing Non-ASCII Strings

my_list = ['Café', 'Niño']
print(ascii(my_list)) # ['Caf\xe9', 'Ni\xf1o']

Example 3: ascii() with Tuples Containing Non-ASCII Strings

my_tuple = ('naïve', 'façade')
print(ascii(my_tuple)) # ('na\xefve', 'fa\xe7ade')

Example 4: ascii() with Dictionaries Containing Non-ASCII Keys and Values

my_dict = {'jalapeño': 'spicy', 'piñata': 'fun'}
print(ascii(my_dict)) # {'jalape\xf1o': 'spicy', 'pi\xf1ata': 'fun'}

Example 5: ascii() with Sets Containing Non-ASCII Strings

my_set = {'résumé', 'cliché'}
print(ascii(my_set)) # {'r\xe9sum\xe9', 'clich\xe9'}

Example 6: ascii() with Non-Printable Characters

non_printable_str = 'Hello\u200bWorld' # Contains a zero-width space
print(ascii(non_printable_str)) # 'Hello\u200bWorld'

All Built in Functions in Python chr() ord()


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer