0

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?

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked Feb 17, 2015 at 12:54
2
  • By the way, 2000^2 == 4000000...not 20002000 Commented Feb 17, 2015 at 12:56
  • 1
    print(type(price)). type() is the best when you find variables behaving oddly. Commented Feb 17, 2015 at 12:59

2 Answers 2

6

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"
answered Feb 17, 2015 at 12:55
Sign up to request clarification or add additional context in comments.

Comments

2

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'
answered Feb 17, 2015 at 12:55

1 Comment

You rock Hacka, always at my help when I'm in need. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.