在Python中统计字符串中数字字符的个数,可以使用以下方法:
1. 使用 `isdigit()` 方法
```python
def count_digits(s):
count = 0
for char in s:
if char.isdigit():
count += 1
return count
s = 'abc123xyz456'
print(count_digits(s)) 输出:6
2. 使用列表推导式和 `str.join()` 方法
```python
def count_digits(s):
return sum(c.isdigit() for c in s)
s = 'abc123xyz456'
print(count_digits(s)) 输出:6
3. 使用正则表达式
```python
import re
def count_digits(s):
return len(re.findall(r'\d', s))
s = 'abc123xyz456'
print(count_digits(s)) 输出:6
以上方法都可以用来统计字符串中数字字符的个数。您可以根据自己的需要选择合适的方法