可以使用Python的isalpha()和isdigit()方法來(lái)統(tǒng)計(jì)字符串中數(shù)字和字母的個(gè)數(shù)。下面是一個(gè)示例代碼:
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"字母?jìng)€(gè)數(shù):{alpha_count}")
print(f"數(shù)字個(gè)數(shù):{digit_count}")
運(yùn)行以上代碼將輸出:
字母?jìng)€(gè)數(shù):5
數(shù)字個(gè)數(shù):3