isdigit 和isnumeric有什么区别
时间: 2023-11-15 16:04:21 浏览: 58
isdigit()和isnumeric()都是Python中的字符串判断函数,其区别如下:
isdigit()函数只能判断Unicode数字,如下表:
| 字符 | 描述 |
| --- | --- |
| 0 | 阿拉伯数字 0 |
| 1 | 阿拉伯数字 1 |
| ... | ... |
| 9 | 阿拉伯数字 9 |
而isnumeric()函数可以判断所有 Unicode 数字,包括阿拉伯数字、中文数字、罗马数字等等。
举例来说:
```python
num1 = '①' # 中文数字1
num2 = '一' # 中文数字1
num3 = 'Ⅰ' # 罗马数字1
print(num1.isdigit(), num1.isnumeric()) # False, True
print(num2.isdigit(), num2.isnumeric()) # False, True
print(num3.isdigit(), num3.isnumeric()) # False, True
```
可以看到,isdigit()函数返回False,而isnumeric()函数返回True。