Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiplication of two arguments in python

Tags:

python

casting

I was trying to use Python and I have created a short method:

def mul(x,y):
    print(x*y)

by running mul(2,"23"), I'm getting this outstanding output : 2323

Can someone explain this?

like image 366
USer22999299 Avatar asked Nov 28 '22 15:11

USer22999299


1 Answers

Because you're multiplying a string and an integer together:

>>> print '23' * 2
2323

Which is equivalent to '23' + '23'.

Do mul(2, int('23')) to turn '23' into an integer, or just get rid of the quotation marks surrounding the 23.

By the way, such function already exists in the operator module:

operator.mul(2, 23)
like image 198
TerryA Avatar answered Dec 05 '22 14:12

TerryA