Home » Programming » Python » Python Isdigit Isalpha

isdigit(), isalpha() and Other Checks in Python, With Examples

Both Python versions 2 and 3 provide several helpful string methods that let you find the string’s contents.  Here’s how to use them.

The functions in this article are useful if you want to validate user input – for example, you may want to check that a user-inputted string is a valid number before converting it to a number variable to prevent errors and warn the user that they need to try again.

isdigit() and isalpha() are commonly used methods to identify a string’s contents, but there are several others built-in to Python, which will also be explored.

These string methods can be called on any string in Python like so:

myString = "Hello Python"
myString.isdigit() # Will return False

Python 2 & 3

The following methods all work in both Python versions 2 and 3, and all return either True or False values:

str.isalnum()

True if all characters in the string are alphanumeric and the string is not empty

str.isalpha()

True if all characters in the string are alphabetic and the string is not empty

str.isdigit()

True if all characters in the string are digits and the string is not empty

str.islower()

True if all characters in the string are lowercase and the string s not empty

str.isspace()

True if there is only whitespace (spaces, tabs, etc.) in the string and the string is not empty

str.istitle()

True if all of the words in the string are capitalized, and the string is not empty

str.isupper()

True if all of the characters in the string are UPPERCASE and the string is not empty

Python 3

Python 3 provides some additional methods to check the contents of a string:

str.isdecimal()

True if all characters are decimal characters (0-9, periods), and the string is not empty

str.isidentifier()

True if the string is a valid identifier. An identifier is the name of a class, function, or variable

str.isnumeric()

True if all characters in the string are numeric, and the string is not empty

str.isprintable()

True, if all the string characters are printable, this includes spaces but excludes other non-printable characters. The string also cannot be empty

For more information about string functions, and information about locales, check out the official docs for your version of Python at:

https://docs.python.org/3.6/library/stdtypes.html#str.isalnum https://docs.python.org/2/library/stdtypes.html#str.isalnum

Examples

Here are some examples of the above methods in use:

myString = "Hello Python"
numString = "12345"
whiteString = "     "
idString = "myString"

myString.isnumeric() # False
myString.isprintable() # True
numString.isdigits() # True
whiteString.isspace() # True
numString.isidentifier() # False
idSString.isidentifier() # True (as there is a variable with that name)

Conclusion

These methods can all be used with Python Boolean Logic to control your application’s flow based on the contents of strings.

Check out our other Python string articles:

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment