How to extract last digit of a number in python
Is there a way to get the last digit of a number. I am trying to find variables that end with "1" like 1,11,21,31,41,etc.. Show If I use a text variable I can simply put
but it works for variables with text(like "hello) but not with numbers. With numbers I get this error:
I am trying to see if there's a better way to deal with numbers this way. I know a solution is to convert to a string and then do the above command but I'm trying to see if there's another way I have missed. Thanks so much in advance... asked Mar 10, 2011 at 2:36
Remainder when dividing by 10, as in
This only works for positive numbers. -12%10 yields 8
answered Mar 10, 2011 at 2:36
Jim GarrisonJim Garrison 84.3k20 gold badges153 silver badges187 bronze badges 0 Use the modulus operator with 10:
This gives the remainder when dividing by 10, which will always be the last digit (when the number is positive). answered Mar 10, 2011 at 2:37
CameronCameron 93.4k21 gold badges194 silver badges221 bronze badges 2 So you want to access the digits in a integer like elements in a list; easiest way I can think of is:
Convert n into a string, accessing last element then use int constructor to convert back into integer. For a Floating point number:
answered Oct 17, 2011 at 5:07
Zv_oDDZv_oDD 1,7701 gold badge17 silver badges26 bronze badges 1 I can't add a comment yet, but I wanted to iterate and expand on what Jim Garrison said Remainder when dividing by 10, as in numericVariable % 10 This only works for positive numbers. -12%10 yields 8 While modulus (%) is working as intended, throw on an absolute value to use the modulus in this situation.
answered Feb 21, 2017 at 21:19
Micah PearceMicah Pearce 1,7213 gold badges24 silver badges55 bronze badges Lostsoul, this should work:
Instead of the print statement at the end, you can add code to do what you need to with numbers ending in 1. Hope it helped! answered Feb 3, 2019 at 3:37
Convert to string first:
answered Dec 15, 2019 at 3:05
JayJay 1,2412 gold badges10 silver badges22 bronze badges The simplest and most efficient way is to use the reminder :
answered Aug 14, 2020 at 17:37
1 This is a simple yet effective way to do it
answered Jun 28 at 10:29
1 By using iteration and the in built function of 'digit' the number is treated as binary and so it goes from backwards to forwards. Here is an example of a bit of code for you.
answered Nov 5, 2017 at 15:02
How do I get the last 4 digits of a number in Python?how to select last 2 elements in a string python. >>>mystr = "abcdefghijkl". >>>mystr[-4:]. 'ijkl'. >>>mystr[:-4] #to select all but last 4 characters.. 'abcdefgh'. How do I get the last 3 digits of a number in Python?Use the modulo operator to get the last 3 digits of a number, e.g. last_three = number % 1000 . The modulo operator will return the last 3 digits of the number by calculating the remainder of dividing the number by 1000. Copied!
How do you extract the last two digits of a number in Python?“get last 2 digits in string python” Code Answer. >>>mystr = "abcdefghijkl". >>>mystr[-4:]. 'ijkl'. >>>mystr[:-4] #to select all but last 4 characters.. 'abcdefgh'. How do you determine the last digit in any number?Modular Arithmetic
Finding the last digit of a number is the same as finding the remainder when this number is divided by 10. In general, the last digit of a power in base n is its remainder upon division by n. So, for decimal numbers, we compute mod 10 to find the last digit, mod 100 to find the last two digits, etc.
|