+ 1
Is there a practical usage in this? I dont get the example as, if its for the sum pourpose, why INT print(int("2") + int("3"))
still learning, so... maybe stupid question
4 Antworten
+ 2
because if you don't specify, it will return strings by default and concatenate them and your result will be 23 instead of 5
+ 2
you have to convert the strings to INT to get the value of the addition of the strings as INT.
in this example, sum would be 5 of type INT.
if you dont convert the strings, print("2" + "3") would print the concatenation of these strings, which is "23".
+ 2
Its useful when you have to add two integer stored like strings because + operator is defined for either (string and int) but with string you concatene they, while with int you add like in math. Example, assume that you have obtained from somewhere two integer from input BUT they are strings:
a= "2"
b= "3
and you want add they but like numbers, if you do
print(a+b)
you will get
23
because you are concatening two strings, while if you do
print(int(a)+int(b))
you will get
5
because you effectively adding two ints (thanks to int function)
+ 2
thank you all, very quick 🙂
I get it