4

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?

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
USer22999299
  • 5,284
  • 9
  • 46
  • 78
  • 1
    When adding Python code to a question, please be careful with the formatting and indentation. – Some programmer dude Nov 01 '13 at 11:47
  • muliply int with a string ? seriously? –  Nov 01 '13 at 11:49
  • 4
    @JohnJohn2 yeah, seriously. I don't think there is anything wrong with the question. – aIKid Nov 01 '13 at 11:52
  • To elaborate on the answers, this behaviour is due to Python's strong [typing system](http://en.wikipedia.org/wiki/Strong_and_weak_typing#Predictability), which might cause "strange" behaviour if you are used to the weak typing and implicit type conversion of, for instance, Javascript. – rlms Nov 01 '13 at 12:07
  • What result were you expecting? Why? – Karl Knechtel Nov 01 '13 at 12:37

6 Answers6

12

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)
TerryA
  • 58,805
  • 11
  • 114
  • 143
5

In your multiplication function, first convert the input to int or float, depending on your requirement.

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

Mulitplication is nothing but repetitive addition. In your case, if a string gets passed to your function, then it gets "added" that many times.

Also, if both the inputs are strings, then it will throw an error.

TypeError: can't multiply sequence by non-int of type 'str'

So it's safest to first convert them, before multiplying.

Sudipta
  • 4,773
  • 2
  • 27
  • 42
4

Multiplication of strings is not the same things as multiplication of numbers.

String multiplication does not implicitly convert to numbers, even if the string could be interpreted as such. Instead, the string is repeated, by the integer number given:

>>> 'foo' * 3
'foofoofoo'

If your method needs to multiply numbers, then make sure you get only numbers; use int() or float() to convert strings to numbers first.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

It's because strings are not automatically converted to integers. Instead multiplication with string returns a string with multiple copies of the original string.

If you want to convert a string to an integer, you have to do it explicitly:

int("23")
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

In Python * operator could operate on both numbers and sequences i.,e either to multiply numbers:

2*3
6

or to repeat sequences:

2*'3'       #string
'33'
[4,6]*2     #list
[4, 6, 4, 6]
(4,8)*2     #tuple
(4, 8, 4, 8)
#won't work with sets ([set type][2]) and dictionaries ([mapping type][3])

This is due to the fact that the operation to be performed by an operator depends upon the operands (objects) as we never declare the types of variables, arguments, or return values in Python.

praba230890
  • 2,192
  • 21
  • 37
0

putting together conditionals, input casting and math

  • update the multiply() function to multiply or divide
  • single parameter is operator with arguments of * or / operator
  • default operator is "*" (multiply)
  • return the result of multiplication or division
  • if operator other than "*" or "/" then return "Invalid Operator"
toti08
  • 2,448
  • 5
  • 24
  • 36