python判断字符为数字还是字母_检测python字符串是数字还是字母

本问题已经有最佳答案,请猛点这里访问。

如何检测字符串中的数字或字母?我知道您使用的是ASCII码,但是什么函数可以利用它们呢?

见此表,尤其是str.isdigit()和str.isalpha()。

检查字符串是否为正数字(整数)和字母表

您可以使用str.isdigit()和str.isalpha()分别检查给定的字符串是否为正整数和字母表。

样品结果:

1

2

3

4

5

6

7

8

9

10

11# For alphabet

>>> 'A'.isdigit()

False

>>> 'A'.isalpha()

True

# For digit

>>> '1'.isdigit()

True

>>> '1'.isalpha()

False

检查字符串是否为正/负-整数/浮点

如果字符串是负数或浮点数,则str.isdigit()返回False。例如:

1

2

3

4

5

6# returns `False` for float

>>> '123.3'.isdigit()

False

# returns `False` for negative number

>>> '-123'.isdigit()

False

如果还要检查负整数和float,则可以编写一个自定义函数来检查它,如下所示:

1

2

3

4

5

6

7

8def is_number(n):

try:

float(n) # Type-casting the string to `float`.

# If string is not a valid `float`,

# it'll raise `ValueError` exception

except ValueError:

return False

return True

样品运行:

1

2

3

4

5

6

7

8

9

10

11

12

13

14>>> is_number('123') # positive integer number

True

>>> is_number('123.4') # positive float number

True

>>> is_number('-123') # negative integer number

True

>>> is_number('-123.4') # negative `float` number

True

>>> is_number('abc') # `False` for"some random" string

False

检查数字时丢弃"nan"(不是数字)字符串

上面的函数将返回"nan"(不是数字)字符串的True,因为对于python来说,它是有效的float,表示它不是数字。例如:

1

2>>> is_number('NaN')

True

为了检查号码是否为"NAN",您可以使用math.isnan()作为:

1

2

3

4

5>>> import math

>>> nan_num = float('nan')

>>> math.isnan(nan_num)

True

或者,如果您不想导入额外的库来检查它,那么您只需使用==将其与自身进行比较即可。当比较nan浮点与自身时,python返回False。例如:

1

2

3# `nan_num` variable is taken from above example

>>> nan_num == nan_num

False

因此,上述函数is_number可以更新为返回"NaN"的False作为:

1

2

3

4

5

6

7

8

9def is_number(n):

is_number = True

try:

num = float(n)

# check for"nan" floats

is_number = num == num # or use `math.isnan(num)`

except ValueError:

is_number = False

return is_number

样品运行:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17>>> is_number('Nan') # not a number"Nan" string

False

>>> is_number('nan') # not a number string"nan" with all lower cased

False

>>> is_number('123') # positive integer

True

>>> is_number('-123') # negative integer

True

>>> is_number('-1.12') # negative `float`

True

>>> is_number('abc') #"some random" string

False

允许将"1+2j"等复数视为有效数字

上面的函数仍然会返回复数的False。如果希望is_number函数将复数视为有效数字,则需要将传递的字符串类型转换为complex(),而不是float()。那么您的is_number函数将如下所示:

1

2

3

4

5

6

7

8

9def is_number(n):

is_number = True

try:

# v type-casting the number here as `complex`, instead of `float`

num = complex(n)

is_number = num == num

except ValueError:

is_number = False

return is_number

样品运行:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18>>> is_number('1+2j') # Valid

True # : complex number

>>> is_number('1+ 2j') # Invalid

False # : string with space in complex number represetantion

# is treated as invalid complex number

>>> is_number('123') # Valid

True # : positive integer

>>> is_number('-123') # Valid

True # : negative integer

>>> is_number('abc') # Invalid

False # : some random string, not a valid number

>>> is_number('nan') # Invalid

False # : not a number"nan" string

PS:根据数字的类型,每次检查的每个操作都会带来额外的开销。选择适合您需求的is_number功能版本。

这是一个非常完整和描述良好的答案。+ 1

回答正确,但不处理类型错误异常。就像你把一个列表传递给is_number函数一样。

@这是按设计的。函数不应隐式压缩异常。例如,如果您执行float([1, 2, 3]),会发生什么?它将引发TypeError例外

对于长度为1的字符串,可以简单地执行isdigit()或isalpha()。

如果字符串长度大于1,则可以生成类似..的函数。

1

2

3

4

5

6def isinteger(a):

try:

int(a)

return True

except ValueError:

return False