In this tutorial, we will explain how to use isalpha Python method with basic syntax by example for better understanding.
What is isalpha in Python? The String isalpha() method returns True if all the string characters are the alphabet; otherwise, it returns False.
Syntax:
<string_value/string_variable>.isalpha()
Input Parameters:
- string_value / String variable : The string that we want to check whether it’s alphabet or not.
Check String using alphabet
We can check the string value it’s numeric or not by simply using the isalpha python method. It means the string value contains only alphabets. one example of an alphabetic value is “Oraask” and a non-alphabetic value is “welcome to Oraask”.
Example (1)
#Print the result of isalpha() method
data='Oraask'
print (''String variable is an alphabetic:', data.isalpha())
Output of example (1)
String variable is an alphabetic: True
In the above example, string variable ‘data’ includes alphabets so when we checked the alphabet data type of it by data.isalpha() in print statement then it shows the result as True.
Example (2)
#Print the result of isalpha() method
msg='welcome to oraask'
print ('String variable is an alphabetic:',msg.isalpha())
Output of example (2)
String variable is an alphabetic: False
In the above example, string variable ‘msg’ includes alphabets so when we checked the alphabet data type of it by msg.isalpha() in print statement then it shows the result as False.
The isalpha() method is not just a tool for checking if a string contains only alphabetic characters; it’s also invaluable in various practical situations. Here are some common use cases where isalpha() proves to be particularly useful:
Validating User Input using isalpha
When collecting user input, especially names or other text fields that should contain only letters, isalpha() helps ensure the data’s integrity.
Example (3):
name = input("Enter your name: ")
if name.isalpha():
print("Valid name.")
else:
print("Name should contain only letters.")
Filtering Lists of Strings using Python isalpha Function
In data processing, you might need to filter out strings that contain non-alphabetic characters.
Example (4):
words = ["Hello", "World123", "Python", "3D"]
alphabetic_words = [word for word in words if word.isalpha()]
print(alphabetic_words)
Output of Example 4:
Output: [‘Hello’, ‘Python’]
Preprocessing Text Data using isalpha Python
text = "Data Science 101"
cleaned_text = ''.join([char for char in text if char.isalpha() or char.isspace()])
print(cleaned_text)
Output of Example 5
Output: ‘Data Science’
In this tutorial, you have learned the string isalpha() method in Python. What is it? And how to check whether the string value is alphabetic or not.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Checking Methods:
- isalnum() : It returns True if all the characters of the string are alphanumeric; otherwise, it returns False.
- isdigit() : It returns True if all the characters of the string are digit; otherwise, it returns False.
- isdecimal() : It returns True if all the characters of the string are decimal; otherwise, it returns False.
- isnumeric() : It returns True if all the characters of the string are numeric; otherwise, it returns False.
- islower() : It returns True if all the string characters are in lowercase; otherwise, it returns False.
- isupper() : It returns False if all the string characters are in an uppercase; otherwise, it returns False.
- isspace() : It returns True if all the characters of a string are whitespace; otherwise, it returns False.
- istitle() : It returns True if all the string words are properly “title cased”; otherwise, it returns False.
- startswith() : It returns True if the string starts with a given substring between the given start and end index; otherwise, it returns False.
- endswith() : It returns True if the string ends with a given substring between start and end index; otherwise, it returns False.
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)