Python isdigit()で文字列型の値が数値かどうか判定する

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

書式
変数名 = "値"
変数名.isdigit()
isdigit()メソッドは指定された値が整数のみで構成されているかどうかを判定します。
ture または false を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
strA = "6688"
if strA.isdigit():
print('true')
else:
print('false')
strA = "6688" if strA.isdigit(): print('true') else: print('false')
strA = "6688"
if strA.isdigit():
      print('true')
else:
      print('false')

実行結果
true

Python

Posted by arkgame