Python isalpha() method
In this post, you will learn about the isalpha() method of the Python programming language.
In most cases, there may be a need to check strings with alphabets only, like in name validation and so on. Python provides many built-in methods for string handling. The isalpha() method is one of them that returns true if all the characters in the string are alphabets. If not, it returns false. This function basically checks if the arguments include only alphabetic letters (both uppercase and lowercase).
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Syntax of isalpha() method
string.isalpha()
Parameters: None
This method does not take any parameter. An error occurs if a parameter is passed.
Return Value from the isalpha() method
The isalpha() method returns the following:
- True, if all characters in the string are alphabets.
- False, if at least one character is not alphabet.
Python isalpha()
The following example demonstrates the working of the isalpha() method.
name = "Priska"
print(name.isalpha())
# contains whitespace
name = "Priska Kashyap"
print(name.isalpha())
# contains number
name = "Priska234232Kasyap"
print(name.isalpha())
Output of the above code:
True
False
False
As you can see in the above example, it returns true only for alphabetic strings. The space is also not considered alphabetic, that's why returns false.
Filter non alphabetic characters using isalpha
This method can also be used to filter non-alphabetic characters. In the given example, we have taken a string that contains non-alphabetic characters. We have looped over the string and filtered the string using the isalpha() method.
string='@Pri#22323ska3Kasy09ap'
count=0
newstring=""
for s in string:
if (s.isalpha()) == True:
count+=1
newstring+=s
print(count)
print(newstring)
Output of the above code:
12
PriskaKasyap
Related Articles
Python Spell Checker ProgramPython remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python smtplib multiple recipients
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml
Python requests GET method
How to convert MySQL query result to JSON in Python
How to fetch data from mongodb using python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas