We frequently need to determine whether a string simply contains letters or not. It would be extremely helpful and time-saving if we could check this problem using an in-built function. The Python isalpha() function can address this problem in just two lines of code. When we just have alphabets as input, the Python isalpha() function comes in handy. Let us understand more about the Python’s isalpha() string class method.
Definition
- The Python isalpha() String function examines a string for alphabetical characters and returns True only if the string contains all alphabetical characters, i.e. alphabets (A-Z, a-z); else returns False.
- Python isalpha() method returns True if all of the characters in the string are alphabets (only letters). If not, it returns False.
Python isalpha()
The Python isalpha() function returns a Boolean value depending on the existence of letters and text characters in the string. In Python, alphabetical characters include uppercase alphabets (A-Z), and lowercase alphabets (a-z). Non-alphabetic characters include whitespace, numerics, and symbols.
isalpha() Syntax
The syntax followed by Python isalpha() is as follows:
string.isalpha()
isalpha() Parameters
Python String isalpha() function does not accept any parameters.
Return value from isalpha()
The isalpha() is responsible for returning a Boolean value:
- True – if all the characters are alphabets in the string
- False – if at least one character is not an alphabetical character and also if there exists whitespaces between them.
Note – If the string is empty, the isalpha() function returns a False value.
Example 1: Working of isalpha()
Example
# Python program to illustrate isalpha()
# contains all alphabets
txt = 'PythonProgramming'
print(txt.isalpha())
# contains whitespace between words
txt = 'Python Programming Language'
print(txt.isalpha())
# contains numbers and alphabets
txt = 'Hello1234'
print(txt.isalpha())
Output
True
False
False
Example 2: Real-life Application of isalpha()
Assume we’re creating a registration form. We can use the Python isalpha() method to ensure that the name entered by the user only contains letters. Here’s an example of a program that would accomplish this task:
Example 1
# Python program to illustrate isalpha()
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
if first_name.isalpha() == True and last_name.isalpha() == True:
   print('All characters are alphabets')
else:
   print('All characters are not alphabets')
Output
Enter your first name: Jimmy
Enter your last name: Johnson
All characters are alphabets
Enter your first name: Sam25
Enter your last name: Dsouza
All characters are not alphabets
Other international languages’ Unicode alphabets can also be identified using the isalpha() technique. It can, for example, examine the German term ‘außen‘.
Example 2
txt = 'außen'
print(txt.isalpha())
Output
True
Frequently Asked Questions
Q1. What is isalpha() in Python?
We frequently need to determine whether a string simply contains letters or not. It would be extremely helpful and time-saving if we could check this problem using an in-built function. The Python isalpha() function can address this problem in just two lines of code. The Python’s isalpha() String function examines a string for alphabetical characters and returns True only if the string contains all alphabetical characters, i.e. alphabets (A-Z, a-z); else returns False.
Q2. How do you use isalpha in Python 3?
The Python isalpha() function returns a Boolean value depending on the existence of letters and text characters in the string. In Python, alphabetical characters includes uppercase alphabets (A-Z), and lowercase alphabets (a-z). The syntax followed by Python’s isalpha() is as follows:
string.isalpha()
The isalpha() is responsible for returning a Boolean value:
- True – if all the characters are alphabets in the string
- False – if at least one character is not an alphabetical character and also if there exists whitespaces between them.
Q3. How do you check if a character is a letter in Python?
To check if all the characters in a string are letters, we use the Python isalpha() function. Python isalpha() method returns True if all of the characters in the string are alphabets (only letters). If not, it returns False.
Example
txt = 'SchoolKids'
print(txt.isalpha())
txt = 'ßackHome'
print(txt.isalpha())
txt = 'School Kids'
print(txt.isalpha())
txt = 'School 254'
print(txt.isalpha())
txt = ' '
print(txt.isalpha())
Output
True
True
False
False
False
Leave a Reply