Posts Python String Isnumeric vs Isdigit
Post
Cancel

Python String Isnumeric vs Isdigit

In python strings comes with multi inbuilt methods. Out of which two of the methods looks close enough but comes with a minor twist.

str.isnumeric() vs str.digit()

Both Method returns True or False based on the input string.

Passing integer as string

1
2
3
4
>> "5".isnumeric()
True
>> "5".isdigit()
True

Passing float as string

1
2
3
4
>> "5.5".isnumeric()
False
>> "5.5".isdigit()
False

Passing string as string

1
2
3
4
>> "abc".isnumeric()
False
>> "abc".isdigit()
False
Wait what?? In above you could see both returned true only for whole number and the results are identical. Then what is the need of 2 different methods.

Purpose of isnumeric

The isnumeric method looks for is the number ia a whole number in all languages (incl non english). Where as isdigit looks just for 0-9

“Eg: In Chinese, they count 1, 2, 3, 4, 5 as 一,二,三,四, 五”

1
2
3
4
>>> "一".isnumeric()
True
>>> "一".isdigit()
False

Hope you find this useful.


Thanks for reading till end.

Followus on insta @techpechu

Join Our Discord

We do run a youtube channel in Indian regional language Tamil called TechPechu. Do subscribe us for moral support. Happy Learning.

Follow us on instagram @techpechu