Reputation: 1581
I'm able to concatenate below 2 integers using below code:
x = 1
y = 2
print(int(str(x) + str(y)))
output:
12
but in below case, it's not working:
x = 0
y = 2
print(int(str(x) + str(y)))
output:
2
Expected output:
02
Many thanks in advance!
Upvotes: 0
Views: 270
Reputation: 26
The int(x)
is used to convert its argument 'x' to an integer. So, when you do str(x)+str(y)
, you get '02', a string data. But when this is passed as an argument to int()
, it becomes 2, because, well 0 has no value when placed before a number (before the decimal).
To get 02, just leave it at str(x)+str(y)
.
Upvotes: 1