Python str.isalpha() function
In Python, the str.isalpha()
method is used to check if all characters in a string are alphabetic (i.e., letters of the alphabet). This method returns True
if all characters in the string are alphabetic and there is at least one character; otherwise, it returns False
.
Syntax
Example Usage
- Basic usage with alphabetic characters:
- Including only letters:
- Checking an empty string:
- With spaces and punctuation:
- Using non-English alphabetic characters:
Important Notes
- The
isalpha()
method only considers letters defined in Unicode, so it can recognize alphabetic characters from various languages, including accented characters. - If the string contains any character that is not a letter (like digits, spaces, punctuation, etc.),
isalpha()
will returnFalse
.
Summary
- Use
str.isalpha()
to check if a string contains only alphabetic characters. - It returns
True
for strings composed solely of letters andFalse
for any string that contains numbers, spaces, or special characters.