can i use y = int("y" + "2") instead of y = int(str(y) + "2") ?.... Let say y = 7.. btw thanks for answering | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can i use y = int("y" + "2") instead of y = int(str(y) + "2") ?.... Let say y = 7.. btw thanks for answering

5th Mar 2017, 12:15 PM
kevin lee
kevin lee - avatar
9 Answers
+ 3
@kevin lee characters have no real meaning in python, but in other languages they are important and without knowledge of a langauge such as Java or C for example, it is hard to explain :/ "y" can't be seen as y because of situation like this : name = "Tom" name=input("name") will write Tom not name, the interpreter can't know which one to display
5th Mar 2017, 5:18 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
no because "y" is a character If it were possible, you would have problem to write a text if there was variable name in it
5th Mar 2017, 4:15 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
what do you mean by int('y')...?
5th Mar 2017, 12:19 PM
vishnu
vishnu - avatar
+ 1
What should "y" have as an integer value ? It is because we can't really answer this question that int("y") has no meaning.
5th Mar 2017, 12:20 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Do you mean the ascii value of 'y'..? you can use >>ord('y') or else for the reverse, you can use >>chr(121)
5th Mar 2017, 12:28 PM
vishnu
vishnu - avatar
+ 1
@vishnu "y" is a string not a character so it does not have an ascii value
5th Mar 2017, 4:16 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
i mean like.. can i use y = int("y" + "2") instead of y = int(str(y) + "2")
5th Mar 2017, 2:53 PM
kevin lee
kevin lee - avatar
0
let say y = 7 i tried the y = int( "y" + "2") and it didnt work.. but it worked for the original coding and get 72 as the output. and then.. i tried another way.. y = "7" y = int( y +"2") print(y) and i got the output 72 same as the original coding. i put "" to the number 7 so it appears as a string for the y integer.. can you explain why? btw what do you mean by 'character'?? thank u..
5th Mar 2017, 5:00 PM
kevin lee
kevin lee - avatar
0
Output of this code is 82. Since, you can easily find the output by executing this code in a python interpreter, I am assuming you need the explanation of the output. So, we know that x=3 and y=8. In the next line, what is done is that the integer value of y is typecasted into a string, so str(y) gives “8”. Now, since the operands involved in addition are both string data type, python interprets the addition sign as concatenation operator. So, “2” is concatenated to “8” and it becomes “82”. You typecast this string “82” to integer value and store in y. So, y now holds the value 82. And then you output y which gives us 82 as the output.
24th Jun 2020, 8:37 AM
aissa roi
aissa roi - avatar