共計 363 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用 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
丸趣 TV 網 – 提供最優質的資源集合!
正文完