阅读量:0
可以使用Python的isalpha()和isdigit()方法来统计字符串中数字和字母的个数。下面是一个示例代码:
def count_alpha_digit(s): alpha_count = 0 digit_count = 0 for char in s: if char.isalpha(): alpha_count += 1 elif char.isdigit(): digit_count += 1 return alpha_count, digit_count s = "Hello123" alpha_count, digit_count = count_alpha_digit(s) print(f"字母个数:{alpha_count}") print(f"数字个数:{digit_count}")
运行以上代码将输出:
字母个数:5 数字个数:3