how do i add a digit to an integer in python ?
value = 9
#add a 9 to "value" to get 99
print(value)
So i want the console to return :
99
I tryed to do
value.append(9)
but it didnt work
I think you could convert your number to a string, add those together, and then convert it back into an integer. Something like
var1 = "9"
var2 = var1 + var1
convertedAnswer = int(var2)
print(convertedAnswer)
could work?
You can convert int to str, concatenate the strings, and the convert back to int:
value = 9
add = 9
print(int(str(value) + str(add)))
print(int(f"{value}{add}")) @Tom Sometimes it is not worth complicating the matter and using the simplest solution.10*123+6 will just result in a bytecode for retrieving a constant. You want something like: timeit.timeit("10*x+y", setup="x=123; y=6") which gives me 0.07322674599999957 and compare that to: timeit.timeit('int(f"{x}{y}")', setup="x=123; y=6") which gives me 0.29948458500000186 so about a 4X speedup
10, then add the new digit value. For example, if you have123and want to add6to the end, you can use10*123 + 6to get1236. It's just arithmetic.