Python String isdigit() 関数は、文字列中の数字文字を調べ、文字列が数字文字のみからなる場合に True を返します。
キーポイント
- 戻り値のタイプ。 ブール値(TrueまたはFalse
- パラメータ値。 パラメータ値: isdigit() 関数では、パラメータを解析する必要はありません。
- 桁と桁の間に空白があると False を返します。
- 空文字列も False を返します。
String isdigit() のシンタックス
str_name.isdigit() |
str_nameは入力文字列を意味します。
また、isdigit()はpythonに内蔵された文字列関数です。
str_name = "12345"
print(str_name.isdigit()) # True
|
String isdigit() の例
以下にさまざまなケースを示します。
ケース 1: 文字列に空白が含まれる場合
str_name = "12 34"
print(str_name.isdigit()) # False
|
ケース2:文字列にアルファベットが含まれる場合
str_name = "Abc123"
print(str_name.isdigit()) # False
str_name = "Abc"
print(str_name.isdigit()) # False
|
ケース3:文字列に特殊文字が含まれる場合
str_name = "@123"
print(str_name.isdigit()) # False
str_name = "@$&"
print(str_name.isdigit()) # False
|
ケース4:文字列に小数が含まれる場合
str_name = "16.7"
print(str_name.isdigit()) # False
|
ケース5:文字列が空である場合
str_name = ' '
print(str_name.isdigit()) # False
|
Pythonで可能なすべての桁の文字のリストを表示するプログラム
Unicodeモジュールを使って、数字文字を確認することができます。
このプログラムは、すべての桁のUnicode文字を表示するものです。
import unicodedata
total_count = 0
for i in range(2 ** 16):
charac = chr(i)
if charac.isdigit():
print(u'{:04x}: {} ({})'.format(i, charac, unicodedata.name(charac, 'UNNAMED')))
total_count = total_count + 1
print("Total Count of Unicode Digit Characters = ",total_count)
|

実際に出力されるのは長いので、ちらっと見ただけです。
Unicodeには445桁の文字があります。