5
1

More than 3 years have passed since last update.

pythonのisdigit()はマイナスの整数を判定できない

Posted at

事の発端

pythonのisdigit()でマイナスの整数を判定しようとしたら、出来なかったので調査した。調査時間10分程度。

参考サイト

https://or3.hatenablog.com/entry/2017/10/08/034955
参考サイトを見たほうがいいです。

サンプルコード

def is_num(a):
    try:
        int(a)
    except:
        return False
    return True


def main():
    s = '-1'
    print('s =', s)
    print('isdecimal:', s.isdecimal())
    print('isdigit:', s.isdigit())
    print('isnumeric:', s.isnumeric())
    print('is_num:', is_num(s))


if __name__ == "__main__":
    main()

実行結果

isdigit.png

感想

isdigitが残念すぎる。

5
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1