Python 字串 isalpha() 方法



Python 字串 isalpha() 方法用於檢查字串是否僅包含字母。如果輸入字串中的所有字元都是字母並且至少包含一個字元,則此方法返回 True。否則,它返回 False。

字母字元是在 Unicode 字元資料庫中定義為“字母”的那些字元,即其通用類別屬性為“Lm”、“Lt”、“Lu”、“Ll”或“Lo”之一。請注意,這與 Unicode 標準中定義的“字母”屬性不同。

讓我們在下一節中更詳細地瞭解此函式。

語法

以下是 Python 字串 isalpha() 方法的語法:

str.isalpha()

引數

Python 字串 isalpha() 方法不包含任何引數。

返回值

如果字串中的所有字元都是字母並且至少包含一個字元,則 Python 字串 isalpha() 方法返回 True,否則返回 False。

示例

小寫字母和大寫字母屬於字母字元。其他字元,如“!”、“@”等,不被視為字母字元。

str = "Hello!welcome"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)

執行上述程式後,將生成以下輸出:

Are all the characters of the string alphabetic? False

示例

只有小寫字母和大寫字母屬於字母字元。

以下是一個 Python 字串 isalpha() 方法的示例。在此程式中,建立了一個字串“Welcome”,並呼叫了 isalpha() 函式。

str = "Welcome"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)

執行上述程式後獲得的輸出如下:

Are all the characters of the string alphabetic? True

示例

只有小寫字母和大寫字母屬於字母字元。甚至空格“ ”也不被視為字母字元。

str = "welcome "
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)

執行上述程式後獲得的輸出如下:

Are all the characters of the string alphabetic? False

示例

只有小寫字母和大寫字母屬於字母字元。

str = "aBCD"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)

上述程式在執行時顯示以下輸出:

Are all the characters of the string alphabetic? True

示例

小寫字母和大寫字母被視為字母字元。甚至數字和空格也不被視為字母字元。

str = "Welcome to Tutorialspoint12"
result=str.isalpha()
print("Are all the characters of the string alphabetic?", result)

上述程式的輸出顯示如下:

Are all the characters of the string alphabetic? False
python_strings.htm
廣告