+ 1

Help required

The example on type convertion does not make sence! Int("3"+"4") What does this mean? If we want to obtain output 34 by adding two strings of "3" and "4", why would we use int function? I thought, the output will be 7, because the int function converts strings "3" and "4" to integers, but the correct answer was 34!

2nd Nov 2016, 6:45 PM
George Smirnov
George  Smirnov - avatar
10 Answers
+ 4
Thank you all, I thing I'm getting somwhere. So, what this functions mean is 1) print("2" + "3") - we just printing a combination of two strings. 2) print(int("2") + int("3")) - we convert strings to integers and add one to another to get the correct mathematical answer 3) print (int("5"+"5")) - we add one string to another and THEN convert the result to integers for whatever reasons.
3rd Nov 2016, 7:52 AM
George Smirnov
George  Smirnov - avatar
+ 3
In programming code within () brackets solved first and then further. so in your case int ("3"+"4") will execute first and both string concatenate and result will be int("34) Now int convert it to 34 which is the answer
2nd Nov 2016, 7:53 PM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 3
This is not completely correct. Yes, parenthesis are always calculated first, but in this case you have a function call not parenthesis. What actually happens here is that the two strings are only one argument to the int() function, and in order to process it has to be calculated first. Essentially is the same thing since in both cases the calculation is going to take place before the function call. int("3"+"4") VS int(("3"+"4"))
3rd Nov 2016, 1:27 AM
Anastasios Glaros
Anastasios Glaros - avatar
+ 2
Anastasios Glaros has the correct answer, as do the others who note it's a string concatenation inside a function call.
3rd Nov 2016, 6:37 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Yeah Anastasios is right .... I forgot about the Int ()function. So Anastasios thanks mate for correcting :)
3rd Nov 2016, 7:03 AM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 2
@George ... Spot on you got it right
3rd Nov 2016, 7:58 AM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 1
First evaluated the string addition that will be "34" and than the Int function convert the "34" string to int and it will be 34.
2nd Nov 2016, 7:03 PM
MihĂĄly CsĂłkĂĄs
MihĂĄly CsĂłkĂĄs - avatar
+ 1
@George Yes. Nicely done.
3rd Nov 2016, 8:00 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
@George - now that you've solved it....as for 'why': people do weird things that make sense (to them, falsely or actually) in context. Here...it's a test; do you know what it does? It doesn't have to make sense here (and it might / might never make sense out in code). Python especially is supposed to be clear so those ought to be documented...precisely because someone like you will come along and think: "what the..?" (and possibly waste time wondering if you should 'fix' it)
3rd Nov 2016, 8:22 AM
Kirk Schafer
Kirk Schafer - avatar
0
As both Waqas and Mihaly said, the first thing done is the concatination of the strings. From there it does the type conversion to int. As for why we may want to do this, the primary reason I would expect is for getting numbers from user input. Since input is a string, we cannot use numbers from input unless they are first converted to int or float.
2nd Nov 2016, 11:23 PM
Luke Armstrong