Get last three digits of an integer

Luke Gatt picture Luke Gatt · Feb 17, 2015 · Viewed 25.6k times · Source

I wish to change an integer such as 23457689 to 689, 12457245 to 245 etc.

I do not require the numbers to be rounded and do not wish to have to convert to String.

Any ideas how this can be done in Python 2.7?

Answer

Warren Weckesser picture Warren Weckesser · Feb 17, 2015

Use the % operation:

>>> x = 23457689
>>> x % 1000
689

% is the mod (i.e. modulo) operation.