String Validators in Python

String Validators in Python

  • 1052

String Validators in Python.This post is an introduction to string validators and assumes no prior knowledge of them. Basic prior knowledge of programming in Python, however, is expected. Let’s get started.

Often times, we work with strings and want to quickly check if they are of a particular type or contain specific data. While we can do the same by either storing the characters of the string in a data structure or explicitly writing down statements to verify, string validators in Python can simplify our task. This can be useful when validating a password string or any other user input. String validators are essentially functions that allow us to check if a string meets certain criteria.

This post is an introduction to string validators and assumes no prior knowledge of them. Basic prior knowledge of programming in Python, however, is expected. Let’s get started.

Checking if a Given String Is Alphanumeric

A string is alphanumeric if all its characters are either digits (0–9) or letters (uppercase or lowercase). The method str.isalnum() checks if the string str is alphanumeric. For example:

"aBcD123".isalnum() #True
"ab*".isalnum() #False
"****".isalnum() #False

Note that the method checks if all the characters in a string are alphanumeric. Thus, it returns false even when it contains non-alphanumeric characters along with alphanumeric characters. Since an empty string doesn’t contain any digit or alphabet, str.isalnum() returns false when applied to it:

"".isalnum() #False

Checking if a Given String Is Alphabetic

A string is alphabetic if all of its characters are alphabets (uppercase or lowercase). The method str.isalpha() checks if the string str is alphabetic. For example:

"abc".isalpha() #True 
"a98".isalpha() #False
"".isalpha() #False

Similar to str.isalnum() , str.isalpha() returns false when applied to an empty string. This is intuitive because the empty string doesn’t contain any alphabets.

Checking if a Given String Is All Numbers

The method str.isdigit() checks whether all the characters of the string str are digits (0–9). For example:

"000".isdigit() #True
"ab0".isdigit() #False
"".isdigit() #False

Checking if a Given String Is Lowercase

The method str.islower() checks whether all the cased characters of a given string str are lowercase. Thus, it returns false even if there is one uppercase letter and returns true if there is none. For example:

"000".islower() #False 
"ra8".islower() #True
"rA".islower() #False
"r*".islower() #True
"".islower() #False

Checking if a Given String Is Uppercase

The method str.isupper() checks whether all the casedcharacters of a given string str are uppercase. Thus, it returns false even if there is one lowercase letter and returns true if there is none. For example:

"000".isupper() #False 
"RA8".isupper() #True
"rA".isupper() #False
"R*".isupper() #True
"".isupper() #False

Checking if a Given String Is Blank

This is a common scenario when building web forms. The method str.isspace() checks if the given string is all whitespace or not. For example:

"".isspace() #False
"   ".isspace() #True
"*".isspace() #False

Intuitively, we refer to this method as “is blank.”

Checking if a Given String Is Title Cased

The method str.istitle() checks if the given string is in title case. In Title Case, all words should start with capital letters and all words should be separated by a whitespace character.

For example:

"000".istitle() #False
"John".istitle() #True
"john".istitle() #False
"CamelCase".istitle() #True
"Camel Case".istitle()

That’s it! We’ve introduced ourselves to string validators and seen how they work.