+ 1

Please explain.

What is happening on line 7 and 10 ? https://code.sololearn.com/c9jVnA2Ud9D6/?ref=app

5th Mar 2020, 10:20 AM
ClassHacker
ClassHacker - avatar
6 Respostas
+ 3
s = "ã" print(s) You assigned 'ã' to s s=ascii("ã") print(s) It will assign the ascii value of ã to s that is \xe3 ss = str(s) Here you converted the value to a string print(type (ss),ss) The type function prints the datatype of ss that is string or str and it will also print the value as you have ss in print too. #<class 'str'> '\xe3' ee = eval(s) After evaluation the ascii code is converted into ã again so the string is simply assigned to ee print(type(ee), ee) Again the type function to print datatype and the value of ee #<class 'str'> ã
5th Mar 2020, 11:10 AM
Utkarsh Sharma
Utkarsh Sharma - avatar
+ 2
Utkarsh Sharma 1. On line 7 why str function is not converting '\xe3' to "ã"❓ 2. Since str('\xe3') == 'ã' then why str(s) ! = 'ã' ❓ Please explain.
6th Mar 2020, 7:29 AM
ClassHacker
ClassHacker - avatar
+ 2
ClassHacker I think it is because of the ascii() function. When you normally assign a = '\xe3' b = str(a) and do print(b) The output will be ã as it will read the string '\xe3' as an ascii code. but if you do a = ascii('\xe3') b =str(a) print(b) It is no longer taken as an ascii code but a normal string. That's why '\xe3' is printed. You can wait for a more knowledgeable user to answer this briefly.
6th Mar 2020, 8:08 AM
Utkarsh Sharma
Utkarsh Sharma - avatar
+ 1
Coder's Crux So why the outputs are different?
5th Mar 2020, 11:07 AM
ClassHacker
ClassHacker - avatar
+ 1
ClassHacker The outputs are different because in ss the value assigned is still ascii code but in ee after eval(s) it is again converted into ã. Both are strings but values are different.
5th Mar 2020, 11:21 AM
Utkarsh Sharma
Utkarsh Sharma - avatar
0
In lines 7 and 10 it converts to string the type of the objects, and then prints the ascii value of the letter
5th Mar 2020, 10:51 AM
coddy
coddy - avatar