Summary: in this tutorial, you’ll learn how to use the Python string isalpha()
method to check if all characters in a string are alphabetic.
Introduction to the Python string isalpha() method #
The string isalpha()
method returns True
if:
- the string contains only alphabetic characters
- and the string has at least one character.
Otherwise, the isalpha()
method returns False
.
The following shows the syntax of the isalpha()
method:
str.isalpha()
Code language: CSS (css)
Note that alphabetic characters are characters defined as letters in the Unicode character database.
Python string isalpha() method examples #
The following example uses the isalpha()
method to check if a string contains alphabetic characters:
name = 'John'
print(name.isalpha())
Code language: PHP (php)
Output:
True
Code language: PHP (php)
It returned True
because the string 'John'
contains only alphabetic characters.
The following example returns False
because the string 'Jane Doe'
contains a space:
name = 'Jane Doe'
print(name.isalpha())
Code language: PHP (php)
Output:
False
Code language: PHP (php)
In general, if the string contains whitespace, the isalpha()
method returns False
.
The following example uses the isalpha()
method to check if all the characters in the string 'Python3'
are alphabetic. It returns False
because the string contains a number:
s = 'Python3'
print(s.isalpha())
Code language: PHP (php)
Output:
False
Code language: PHP (php)
The following example returns False becuase the string is empty:
empty = ''
print(empty.isalpha())
Code language: PHP (php)
Output:
False
Code language: PHP (php)
Summary #
- Use the Python string
isalpha()
method to check if all characters in a string are alphabetic.