Remove Last Number of a Integer

Question:

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)
Asked By: user7513134

||

Answers:

That is very simple:

operationTwo = int(operationTwo / 10)
Answered By: Silveris

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)
Answered By: Mohd

I want to remove last three digits from a timestamp and Mohd’s answer work for me.
Before using this I got this error OSError: [Errno 22] Invalid argument

data = 1659347766123
data = int(str(data)[:-3])
print(data)
timeStamp = datetime.fromtimestamp(data)

print(timeStamp)

// 1659347766
// 2022-08-01 15:26:06
Answered By: akshay_sushir
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.