Python3 isalpha() Method
Description
The Python isalpha() method checks whether a string consists only of letters or characters.
- Empty string: If the string is empty (length 0), it returns
False, because an empty string contains no letters. - Case sensitivity:
isalpha()The method is case-sensitive, meaning only alphabetic characters are considered valid. - Special characters: Special characters (such as punctuation, spaces, digits, special symbols, etc.) are not considered letters.
Syntax
isalpha() method syntax:
str.isalpha()
Parameters
- None.
Return Value
Returns True if the string has at least one character and all characters are letters or characters, otherwise returns False.
Example
The following example demonstrates the isalpha() method:
Example
#!/usr/bin/python3
str = "example"
print (str.isalpha())
# Letters and Chinese characters
str = "example Rookie Tutorial"
print (str.isalpha())
str = "Example example....wow!!!"
print (str.isalpha())
str = "example"
print (str.isalpha())
# Letters and Chinese characters
str = "example Rookie Tutorial"
print (str.isalpha())
str = "Example example....wow!!!"
print (str.isalpha())
The output of the above example is:
True True False
More examples:
Example
# Example 1: String containing only letters
print("HelloWorld".isalpha()) # Output: True
# Example 2: String containing digits
print("Hello123".isalpha()) # Output: False
# Example 3: String containing spaces
print("Hello World".isalpha()) # Output: False
# Example 4: String containing special characters
print("Hello#World".isalpha()) # Output: False
# Example 5: Empty string
print("".isalpha()) # Output: False
# Example 6: String containing underscores
print("Hello_World".isalpha()) # Output: False
# Example 7: String containing non-ASCII letters
print("HélloWorld".isalpha()) # Output: True, because é is a letter
print("HelloWorld".isalpha()) # Output: True
# Example 2: String containing digits
print("Hello123".isalpha()) # Output: False
# Example 3: String containing spaces
print("Hello World".isalpha()) # Output: False
# Example 4: String containing special characters
print("Hello#World".isalpha()) # Output: False
# Example 5: Empty string
print("".isalpha()) # Output: False
# Example 6: String containing underscores
print("Hello_World".isalpha()) # Output: False
# Example 7: String containing non-ASCII letters
print("HélloWorld".isalpha()) # Output: True, because é is a letter
Other Extensions
Python3 Strings