可以使用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