pythonif-statementdigit

How to get the second digit of a number or string in python?


Is there any good idea how to get the second digit of a string? For example:

aaa = 122
bbb = 333

rest = bbb-aaa 
if rest[:2] == 1: 
    do something..

Solution

  • Convert the integer to a string first using the built-in str() function and then slice it accordingly

    So Try this:

    if str(rest)[1] == '1':
        #do something example:
        print 'hi'