Hướng dẫn remove last number python

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]

Right leg

15.3k6 gold badges45 silver badges75 bronze badges

asked May 30, 2017 at 15:14

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 May 30, 2017 at 15:19

MohdMohd

5,4207 gold badges18 silver badges29 bronze badges

1

That is very simple:

operationTwo = int[operationTwo / 10]

answered May 30, 2017 at 15:15

SilverisSilveris

1,00812 silver badges28 bronze badges

6

Remove the last Digit from an Integer in Python #

To remove the last digit from an integer, use floor division to divide the integer by 10, e.g. result_1 = my_int // 10. Floor dividing by 10 will strip the last digit from the integer.

Copied!

my_int = 1357 result_1 = my_int // 10 print[result_1] # 👉️ 135 # ------------------------------------------ result_2 = int[str[my_int][:-1]] print[result_2] # 👉️ 135

The first example floor-divides the integer by 10 to remove the last digit.

Division / of integers yields a float, while floor division // of integers results in an integer.

The result of using the floor division operator is that of a mathematical division with the floor[] function applied to the result.

Note that Python integers are immutable, so the only way to remove a digit from an integer is to create a new integer that doesn't contain the digit.

An alternative approach is to use string slicing.

To remove the last digit from an integer:

  1. Use the str[] class to convert the integer to a string.
  2. Use string slicing to get a slice of the string up to the last digit.
  3. Use the int[] class to convert the string to an integer.

Copied!

my_int = 1357 result_2 = int[str[my_int][:-1]] print[result_2] # 👉️ 135

We used the str[] class to convert the integer to a string, so we can get a slice of the string.

The syntax for string slicing is my_str[start:stop:step] where the start value is inclusive, and the stop value is exclusive.

The stop index in the example is negative, meaning go up to, but not including the last character in the string.

Copied!

my_int = 1357 print[str[my_int][:-1]] # 👉️ '135'

If you need to remove the last two digits from the integer, you can update the stop index to -2.

Copied!

my_int = 1357 print[str[my_int][:-2]] # 👉️ '13'

The last thing we have to do is convert the string to an integer.

Copied!

my_int = 1357 result_2 = int[str[my_int][:-1]] print[result_2] # 👉️ 135

The int class returns an integer object constructed from the provided number or string argument.

Chủ Đề