Skip to content
Advertisement

Remove Last Number of a Integer

I have searched for a few answers and none of them seems to work.

Below is an example but the code does not work and gives errors. I am using Python 2.7.

operationTwo = 91239
operationTwo = operationTwo[:-1]
print(operationTwo)

Advertisement

Answer

The code you found which is slicing, works but not on integers. If you want to use it you can convert the number to str for slicing then convert it back to int. It is not the best practice but it can be done as the following:

operationTwo = 91239
operationTwo = int(str(operationTwo)[:-1])
print(operationTwo)

I would however go with integer division, like:

operationTwo = 91239
operationTwo = operationTwo // 10
print(operationTwo)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement