Home > Software engineering >  How to remove a single number from input number in python
How to remove a single number from input number in python

Time:02-15

Here are few example input & outputs to understand the question

example 1 input = 555 output should be = 55

example 2

input = 5455 output should be = 545

example 3

input = 6555 output should be = 655

example 4

input = 3675 output should be = 367

Kindly help me with code in python asap

CodePudding user response:

If those are proper numbers, then x // 10 (integer division by 10) should do the trick. If they are strings, then x[:-1] gets rid of the last character. In both cases x is the variable holding the original value.

CodePudding user response:

You may simply divide by 10 and then cast to integer:

inp = [555, 5455, 6555, 3675]
output = [int(x / 10) for x in inp]
print(output)  # [55, 545, 655, 367]

The above works if your inputs are actual numbers, and not strings. If the inputs are not strings, then you certainly should not convert them to string first, since the above will perform much faster than a bulky string operation.

  • Related