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 Sensitive:
isalpha()The method is case-sensitive in the sense that only alphabetic characters are considered valid. - Special characters: special characters (such as punctuation, spaces, digits, special symbols, etc.) are not considered letters.
Syntax
Syntax of the isalpha() method:
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
=
"runoob"
(
str
.
isalpha
(
)
)
Letters and Chinese characters
str
=
"runoobRunoob Tutorial"
(
str
.
isalpha
(
)
)
str
=
"Runoob example....wow!!!"
(
str
.
isalpha
(
)
)
The output of the above example is:
True
True
False
More examples:
Example
Example 1: a string containing only letters
(
"HelloWorld"
.
isalpha
(
)
)
Output: True
Example 2: a string containing digits
(
"Hello123"
.
isalpha
(
)
)
Output: False
Example 3: a string containing spaces
(
"Hello World"
.
isalpha
(
)
)
Output: False
Example 4: a string containing special characters
(
"Hello#World"
.
isalpha
(
)
)
Output: False
Example 5: an empty string
(
""
.
isalpha
(
)
)
Output: False
Example 6: a string containing an underscore
(
"Hello_World"
.
isalpha
(
)
)
Output: False
Example 7: a string containing non-ASCII letters
(
"HélloWorld"
.
isalpha
(
)
)
Python3 Strings