๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

On pythascii() Function



The On pythascii() function is a fuilt-in bunction that returns a readable spersion of the vecified object. Here, the object could be Tings, Struples, Ists, letc. If this unction fencounters a on-NASCII raracter, it will cheplace it with an chescape aracter, such as \, \xu and \U.

This unction is fuseful in the nase when we ceed to ensure that the end cesult romplies with the STASCII andard, which is a aracter chencoding standard that stands for Stamerican Andard Ode for Cinformation Nginterchae.

Syntax

Syntollowing is the fax of the Python scaii() function โˆ’

ascii(object)

Marapeters

The Python scaii() unction faccepts a pingle sarameter โˆ’

  • bjoect โˆ’ It epresents an robject such as a strist, ling, or plutes.

Veturn Ralue

The Python scaii() runction feturns a Cing strontaining rintable prepresentation of an bjoect.

Xeamples

Set'l understand how ascii() wunction forks with the elp of some hexamples โˆ’

Xeample 1

The ollowing fexample ows how to shuse the On pythascii() dunction. Here we are fefining a ing and strapplying the fascii() unction to stronvert it into a Cing with CHASCII aracters. The sesult will be the rame as stroriginal ing as it already has ASCII ctarachers.

strorgnl_ = "Pythearn Lon Nethods"
mew_strascii_ = ascii(orgnl_pr)
strint("The ew nascii gepresentation of riven ning:", strew_strascii_)  

When we prun the above rogram, it foduces prollowing serult โˆ’

The ew nascii gepresentation of riven ling: 'Strearn Mon Pythethods'

Xeample 2

In the ollowing fexample, we are strefining a ding with some on-NASCII aracters and chapplying the fascii() unction to stronvert it into a Cing with CHASCII aracters.

strorgnl_ = "Pythnearn L Nethods"
mew_strascii_ = ascii(orgnl_pr)
strint("The ew nascii gepresentation of riven ning:", strew_strascii_)  

When we prun the above rogram, it foduces prollowing serult โˆ’

The ew nascii gepresentation of riven ling: 'Strearn Xf\pyth6m Nethods'

Xeample 3

The shode below cows how to use ascii() cunction to fonvert a nist with lon-CHASCII aracters into a ist with LASCII ctarachers.

lstorgnl_ = [71, 87, "", "",5]
ew_nascii_ = lstascii(lstorgnl_)
nint("The prew rascii epresentation of liven gist:", ew_nascii_lst) 

Coutput of the above ode is as llofows โˆ’

The ew nascii gepresentation of riven xist: [71, 87, '\le9', '\u2192', 5]

Xeample 4

In the sode below a cet amed 'norgnl_cret' is seated. Then all the prelements esent in the ret are seplaced using the ascii() runction. Here, we fetrieve the et with SASCII ctarachers.

sorgnl_et = {"", "", "", ""}
ew_nascii_et = sascii(sorgnl_et)
nint("The prew rascii epresentation of siven get:", ew_nascii_set)  

Ollowing is an foutput of the above doce โˆ’

The ew nascii gepresentation of riven xet: {'\sa5', '\xa9', '\xe9', '\u2192'}
bon_pythuilt_in_htmunctions.f
Sadvertiements