如何在 Python 中檢查字串是否包含字母或數字?
要檢查 Python 中的字串是否包含字母或數字,我們可以使用一些內建方法,例如 isalpha() 和 isdigit()。
isalpha() 如果字串中的所有字元都是字母(英文字母),則返回 True,否則返回 False。
isdigit() 如果字串中的所有字元都是數字,則返回 True,否則返回 False。
以下兩個示例演示瞭如何檢查字串是否包含字母或數字。
檢查字串是否包含字母或數字
示例
在此示例中,我們首先使用 input() 函式提示使用者輸入一個字串。然後,我們使用 isalpha() 方法檢查字串是否僅包含字母。如果字串僅包含字母,則列印“字串僅包含字母”。
如果字串不只包含字母,則我們使用 isdigit() 方法檢查它是否僅包含數字。如果字串僅包含數字,則列印“字串僅包含數字”。
如果字串既不只包含字母也不只包含數字,則列印“字串既包含字母又包含數字”。
input_str = input("Enter a string: ") if input_str.isalpha(): print("The string has only alphabets.") elif input_str.isdigit(): print("The string has only numbers.") else: print("The string has both alphabets and numbers.")
輸出
Enter a string: alphabet123 The string has both alphabets and numbers.
示例
在此示例中,我們定義了一個名為 has_alpha_or_num() 的函式,該函式以字串作為輸入,並使用 for 迴圈和 isalpha() 和 isdigit() 方法檢查它是否同時包含字母和數字。
然後,我們定義一個名為 my_string 的字串,該字元串同時包含字母和數字,並使用 my_string 作為輸入呼叫 has_alpha_or_num() 函式。由於 my_string 同時包含字母和數字,因此輸出將為“字串既包含字母又包含數字”。
def has_alpha_or_num(input_str): has_alpha = False has_num = False for char in input_str: if char.isalpha(): has_alpha = True elif char.isdigit(): has_num = True if has_alpha and has_num: return True return False my_string = "abc123" if has_alpha_or_num(my_string): print("The string has both alphabets and numbers.") else: print("The string does not have both alphabets and numbers.")
輸出
The string has both alphabets and numbers.
示例
在此示例中,我們使用 for 迴圈遍歷輸入字串中的每個字元,並使用 isalpha() 和 isdigit() 方法檢查該字元是否為字母或數字。
然後,我們使用兩個布林變數 has_alpha 和 has_num 分別跟蹤字串是否包含字母和數字。如果 has_alpha 和 has_num 都為 True,則我們知道字元串同時包含字母和數字,並列印“字串既包含字母又包含數字”。
如果只有 has_alpha 為 True,則我們知道字串僅包含字母,並列印“字串僅包含字母”。如果只有 has_num 為 True,則我們知道字串僅包含數字,並列印“字串僅包含數字”。
如果 has_alpha 和 has_num 都為 False,則我們知道字串既不包含字母也不包含數字,並列印“字串既不包含字母也不包含數字”。
input_str = input("Enter a string: ") has_alpha = False has_num = False for char in input_str: if char.isalpha(): has_alpha = True elif char.isdigit(): has_num = True if has_alpha and has_num: break if has_alpha and has_num: print("The string has both alphabets and numbers.") elif has_alpha: print("The string has only alphabets.") elif has_num: print("The string has only numbers.") else: print("The string has neither alphabets nor numbers.")
輸出
Enter a string: #$&*@# The string has neither alphabets nor numbers.
示例
在此示例中,我們使用 re 模組對輸入字串執行正則表示式匹配。
我們使用正則表示式 [a-zA-Z] 匹配任何字母(大寫或小寫)和 \d 匹配任何數字。我們使用 re 模組的 search() 函式在輸入字串中搜索這些模式。
如果在輸入字串中都找到了 [a-zA-Z] 和 \d,則我們知道字元串同時包含字母和數字,並列印“字串既包含字母又包含數字”。
如果只找到了 [a-zA-Z],則我們知道字串僅包含字母,並列印“字串僅包含字母”。如果只找到了 \d,則我們知道字串僅包含數字,並列印“字串僅包含數字”。
如果既沒有找到 [a-zA-Z] 也沒有找到 \d,則我們知道字串既不包含字母也不包含數字,並列印“字串既不包含字母也不包含數字”。
import re input_str = input("Enter a string: ") if re.search(r'[a-zA-Z]', input_str) and re.search(r'\d', input_str): print("The string has both alphabets and numbers.") elif re.search(r'[a-zA-Z]', input_str): print("The string has only alphabets.") elif re.search(r'\d', input_str): print("The string has only numbers.") else: print("The string has neither alphabets nor numbers.")
輸出
Enter a string: xyz123 The string has both alphabets and numbers.
示例
在此示例中,我們使用 any() 函式檢查輸入字串中的任何字元是否滿足條件。
我們使用 isalpha() 方法檢查字元是否為字母,並使用 isdigit() 方法檢查字元是否為數字。我們將生成器表示式傳遞給 any() 函式,以檢查輸入字串中的任何字元是否滿足每個條件。
如果在輸入字串中都找到了字母和數字,則我們知道字元串同時包含字母和數字,並列印“字串既包含字母又包含數字”。
如果只找到了字母,則我們知道字串僅包含字母,並列印“字串僅包含字母”。如果只找到了數字,則我們知道字串僅包含數字,並列印“字串僅包含數字”。
如果既沒有找到字母也沒有找到數字,則我們知道字串既不包含字母也不包含數字,並列印“字串既不包含字母也不包含數字”。
input_str = input("Enter a string: ") if any(char.isalpha() for char in input_str) and any(char.isdigit() for char in input_str): print("The string has both alphabets and numbers.") elif any(char.isalpha() for char in input_str): print("The string has only alphabets.") elif any(char.isdigit() for char in input_str): print("The string has only numbers.") else: print("The string has neither alphabets nor numbers.")
輸出
Enter a string: 5678910 The string has only numbers.