Search code examples
pythonbase36

Check if a string is a base36 id?


Assuming I have a function base36-2check,

And a user can input a string, how would I check that that string is base36?


Solution

  • Catch the int constructor's exception, like so:

    In [3]: try:
       ...:     int('abc!d', 36)
       ...: except ValueError:
       ...:     print "Nope"
    

    Sample program:

    def base36_check(s):
        try:
            int(s, 36)
            return True
        except ValueError:
            return False
    
    
    print base36_check('abcdefg')
    print base36_check('abc!efg')