The Python
isalpha()
,
isnumeric()
and
isalnum()
are three string methods that return boolean value True is the string only containing alphabets, numbers, and alphabets & numbers, respectively.
While working with Python string values, we often come across situations where we need to perform some action based on the data value present in the string. For instance, we want to convert a string value into an integer if the string only contains a numeric value. Or you want to check if the username string value entered by the user is a valid input with no special characters. For such string value checks, we have three methods for Python string,
isalpha()
,
isnumeric()
and
isalnum()
. In this tutorial, we will walk through these three Python methods and learn how to use them. So let's get started.
Python string isalpha() method
The
isalpha()
method stands for "is alphabets?", this method returns a boolean value True if all the characters inside the string value are alphabets. Even if a single character inside the string value is a non-alphabet, such as white space, numeric value and special symbols, the method will return False
Syntax
string.isalpha()
The
isalpha()
does not accept any argument and returns a Boolean value (True or False).
Example
string1 = "ThisStringContainsOnlyAlphabets" #True string2 = "This string contains alphabets > symbol and white space" string3 = "This string contains alphabets and numbers 1234" #False string4 = "12345" #False print("Is string1 all alphabets?: ", string1.isalpha()) print("Is string2 all alphabets?: ", string2.isalpha()) print("Is string3 all alphabets?: ", string3.isalpha()) print("Is string4 all alphabets?: ", string4.isalpha())
Output
Is string1 all alphabets?: True Is string2 all alphabets?: False Is string3 all alphabets?: False Is string4 all alphabets?: False
Python string
isnumeric()
method
The
isnumeric()
string method stands for "is numeric?". This method does not accept any argument value and returns a boolean value True or False after checking the string. The method
isnumeric()
returns True if all the characters inside the string are numbers. If any of the characters inside the string is an alphabet, special symbol, dot, or white space, the
isnumeric()
method will return False.
Syntax
string.isnumeric()
Example
string1 = "123,453" #False containing , string2 = "1 234 567" #False containing white space string3 = "123.34" #False containing . string4 = "12345" #True all numbers print("Is string1 all Numbers?: ", string1.isnumeric()) print("Is string2 all Numbers?: ", string2.isnumeric()) print("Is string3 all Numbers?: ", string3.isnumeric()) print("Is string4 all Numbers?: ", string4.isnumeric())
Output
Is string1 all Numbers?: False Is string2 all Numbers?: False Is string3 all Numbers?: False Is string4 all Numbers?: True
Python string
isalnum()
method
The python
isalnum()
is a string method that stands for "is alphabets or numbers?". This method is a combination of
isalpha()
and
isnumeric()
.
isalnum()
also does not accept any argument value and return a boolean value after checking all the string characters. This method returns True if all the characters inside the string are either alphabets or numbers. If the string contains any character other than alphabets or numbers then this method returns False.
syntax
string.isalnum()
Example
string1 = "ABC" #True all alphabets string2 = "1234" #True all numbers string3 = "123ABCD" #True alphabets and numbers string4 = "12345 ABC" #False cotaining white space print("Is string1 all AlphaNumeric Values?: ", string1.isalnum()) print("Is string2 all AlphaNumeric Values?: ", string2.isalnum()) print("Is string3 all AlphaNumeric Values?: ", string3.isalnum()) print("Is string4 all AlphaNumeric Values?: ", string4.isalnum())
Output
Is string1 all AlphaNumeric Values?: True Is string2 all AlphaNumeric Values?: True Is string3 all AlphaNumeric Values?: True Is string4 all AlphaNumeric Values?: False
Conclusion
isalpha(), isnumeric() and isalnum() are the three most used Python string's methods. These three methods are widely used in string data analysis, although for most of the string analysis we always prefer to use regular expression, but string methods like these save a lot of time and make it easy for us to check the data. The isalpha() checks if the string contains only alphabets characters, the isnumeric() method checks if all the characters are numbers and the isalnum() checks if the string only contains alphabets or numbers.
People are also reading:
- Python Dictionary Get: A Complete Guide
- Move File in Python: A Complete Guide
- Flatten List & List of Lists in Python
- Guide for Starting Python Coding on a MacBook
- What is CherryPy Python?
- How to Get Open Port Banner in Python?
- Extract Wikipedia Data in Python
- How to Use Github API in Python?
- Python map() function
- Reading and Writing CSV Files in Python
Leave a Comment on this Post