I want to get the amount of a product from a string and multiply it by the product quantity, so I did
>>>trip='Standard Price:2000'
>>>price = trip.split(":")[1]
'2000'
I do the maths here
>>>price*2
But the answer I should get is '4000' not the one below:
>>>20002000
Isn't * symbol a multiplication sign? because when I checked that's the symbol don't know why I'm getting the raise to power answer. What did I miss?
2 Answers 2
Convert to int first:
int(price) * 2 # result: 4000
Then you can use mathematical operators as you know them. Otherwise you are multiplying strings:
"x" * 10 # result: "xxxxxxxxxx"
Comments
you need to convert to int before multiplication:
>>> int(price)*2
if you multiply string with a number it will produce string that number of times:
Demo:
>>> 'a'*4
'aaaa'
print(type(price)).type()is the best when you find variables behaving oddly.