Skip to content
Advertisement

I want to distinguish between true digit and a string digit

I want to check a ‘x’ string whether it is a digit or not in advance.

‘1’ is naturally a digit.

But and I will use ① what is calld a string number very much.

I don’t know the range of string numbers IDE judges as a digit.

'①'.isdigit() returns True.

'⑴'.isdigit() returns True.

'ⅰ' or 'Ⅰ' returns False.

'㈠' returns False. (kanji version of (1) )

'❶' returns True.

I want to do like this.

for s in data:
    if s.isdigit():
       int_ = int(s)

If I accept '①', int will throw an error. Now, I write try:except for it.

Because I’m a japanese, I often use '①' or '⑴'

How to distinguish isdigit or not isdigit in advance?

Should I rely on try:except or counting all of them in advance?

regular expression?

The main problem is I don’t know what is judged as a digit.

data = ["1", "23", "345", "①", "(1)", "(2)"]

This data is dynamic value. It will be changed every time.

Moreover, the string like this may expand in the future.

I hope the string of isdigit() == True is accepted by int().

I don’t have an urgent problem because of try: except.

Advertisement

Answer

I believe that the str.isdecimal method fits your requirements. It excludes strings like '①', but includes other strings like '١' which are accepted by int.

>>> int('١')                
1
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement