Home > database >  extracting second digit from a number
extracting second digit from a number

Time:05-25

In my program, I have a data file with a column processing type. This column has numbers datatypes as object. I want to write a condition that if the number has 2 as a 2nd digit , then i want to keep these records rest filtered outenter image description here

CodePudding user response:

a = str(YOUR_NUMBER)[::-1][2]
answer = int(a[2])

This converts your number to a string, reverses it, and then gets the second character, and converts it back to a number. the variable answer contains what you want.

CodePudding user response:

If by 'second digit' you mean the penultimate one (ex: for 1234 it would be 3)

Then you can simply do second_digit = (n // 10) % 10 or second_digit = int(str(n)[-2]) where n is your number

  • Related