In this tutorial, we’ll learn what Python string isalpha() method is and how to properly use it with the help of an easy and detailed multiple Python code examples.
Introduction: Python String isalpha() Method
This method is used to return True if all the characters of specified string are alphabets. If not, then it will return False.
Syntax of isalpha() Method
string.isalpha()
- This method does not take any arguments.
- Python string isalpha() method returns True if the string consist of only alphabets, or else it returns False.
Example 1: Applying isalpha() Method on a Python String
str='PythonLanguage' print( str.isalpha() )
Output
True
As we can see that True is returned by isalpha method. It means the specified string only has alphabets. Let’s now take a look at other examples where the string will consist of other characters as well.
print('Python Language'.isalpha()) print('Python3Language'.isalpha()) print('Python3 Language'.isalpha()) print('Python Language *'.isalpha()) print('Python_Language'.isalpha()) print('Python-Language'.isalpha())
Output
False False False False False False
By using any other character(except alphabet) inside string will result in False returned by Python string isalpha method.
Example 2: isalpha() Method in Python For Loop and If Else Condition
listOfValues=['Python','Programing','This is Python','Python Programming','PythonProgramming3'] for val in listOfValues: if val.isalpha(): // same like if val.isalpha()==True: print(val+' - It consists of only alphabets ') else: print(val+' - It doesnot consists of only alphabets')
Output
Python - It consists of only alphabets Programing - It consists of only alphabets This is Python - It doesnot consists of only alphabets Python Programming - It doesnot consists of only alphabets PythonProgramming3 - It doesnot consists of only alphabets
We’ve created a Python list and have stored some strings in it. Then we’ve implemented a Python for loop with an if else condition. This program will check each and every item of list. If the item consist of only alphabets then body of if statement will be executed. If not, then body of else statement will be triggered.
Example 3: isalpha() Return Value
str='PythonProgrammingLanguage' returnedValue=str.isalpha() print(returnedValue)
Output
True
As we can see in the above output that Python string isalpha method returns True as the specified string has only alphabets in it.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string isalpha method. I’ll love to receive your valuable feedback on this post. Thank you for reading it.