你可以通過使用 count() 方法來統計字符串中特定字符的個數。下面是一個示例代碼,可以統計字符串中字母的個數:
string = input("請輸入字符串:")
letter_count = 0
for char in string:
if char.isalpha():
letter_count += 1
print("字母個數為:", letter_count)
在這個示例中,我們首先使用 input() 函數獲取用戶輸入的字符串。然后,我們使用一個 for 循環遍歷字符串中的每個字符。對于每個字符,我們使用 isalpha() 方法來判斷它是否為字母。如果是字母,則將 letter_count 變量加一。最后,我們打印出統計結果。