Count Digits and Letters in Python
You can use the isalpha() and isdigit() methods in Python to count the number of letters and digits in a string. Here is an example code:
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}")
Running the above code will result in:
字母个数:5
数字个数:3