Python Code - Strings method

Hello I’m writing this code below and its returning a string.

the cost of apples

cost_of_apple = 2

Amount of apples

amount_of_apples = input("How many apples do you want? ")

total_sum = cost_of_apple * amount_of_apples

print("You have to pay: " + str(total_sum))

print(total_sum)

Its printing = 2222 # so the double of the actual result. can someone please clarify why its coming out like that.

Regards.

In order for it to work I had to edit my code

amount_of_apple = int(input("How many apples do you want? "))
I made the input an integer from the beginning.

Thanks for sharing. @timokito

I’m assuming you entered ‘4’ when the program prompted you for “How many apples do you want”.

When using the * operator on strings, it just duplicates it a couple of times. Using the int() function, you can transform a string to a number. That’s why it works like that.
Open a Python REPL, and you can quickly play around with it:

>>> my_text = '2'
>>> my_number = 2
>>> my_text * 4
'2222'
>>> my_number * 4
8
>>> int(my_text) * 4
8

Hello @HerrSubset
Thanks for sharing!