A string in Python is a sequence of characters. You can perform various operations on strings using a set of built-in methods. String boolean methods are a subset of these built-in methods used to check if the given string follows certain rules or not.

In this article, you'll learn how to use several different string boolean methods in Python 3 to become a more efficient developer.

How to Check if a String Contains Only Alphanumeric Characters

You can check if the given string consists of only alphanumeric characters using the isalnum() method.

This method returns True if all the characters are alphanumeric. Alphanumeric characters are (A-Z), (a-z), and (0-9). If any of the characters in the string are not alphanumeric, this method returns False.

Examples:

        str1 = "WelcomeToMUO"
# This method will return "True" as all the characters are alphanumeric
checkstr1 = str1.isalnum()
print(checkstr1)

str2 = "Welcome To MUO"
# This method will return "False" as the string have 2 whitespaces which are not alphanumeric
checkstr2 = str2.isalnum()
print(checkstr2)

str3 = "#WelcomeToMUO"
# This method will return "False" as the string have a special character "#" which is not alphanumeric
checkstr3 = str3.isalnum()
print(checkstr3)

str4 = "274962472"
# This method will return "True" as all the characters are alphanumeric
checkstr4 = str4.isalnum()
print(checkstr4)

str5 = "Welcome2MUO"
# This method will return "True" as all the characters are alphanumeric
checkstr5 = str5.isalnum()
print(checkstr5)

Output:

        True
False
False
True
True

How to Check if a String Consists of Only Alphabetic Characters

You can check if the given string consists of only alphabetic characters using the isalpha() method.

This method returns True if all the characters are alphabetic. Alphabetic characters are (A-Z) and (a-z). If any of the characters in the string are not alphabetic, this method returns False.

Examples:

        str1 = "WelcomeToMUO"
# This method will return "True" as all the characters are alphabetic
checkstr1 = str1.isalpha()
print(checkstr1)

str2 = "Welcome To MUO"
# This method will return "False" as the string have 2 whitespaces which are not alphabetic
checkstr2 = str2.isalpha()
print(checkstr2)

str3 = "#WelcomeToMUO"
# This method will return "False" as the string have a special character "#" which is not alphabetic
checkstr3 = str3.isalpha()
print(checkstr3)

str4 = "274962472"
# This method will return "False" as all the characters are not alphabetic
checkstr4 = str4.isalpha()
print(checkstr4)

str5 = "Welcome2MUO"
# This method will return "False" as the string has a digit "2" which is not alphabetic
checkstr5 = str5.isalpha()
print(checkstr5)

Output:

        True
False
False
False
False

How to Check if All Characters in a String Are Decimals, Digits, or Numeric

You can check if the given string consists of only decimal characters, digits, or numeric characters using the isdecimal(), isdigit(), and isnumeric() methods respectively. All three methods seem to be similar, but the Python documentation notes the difference between the three methods as:

  • isdecimal(): Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
  • isdigit(): Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits that cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
  • isnumeric(): Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

By definition, the relationship between the three methods can be represented as:

        isdecimal() ⊆ isdigit() ⊆ isnumeric()
    

This means, if a string is a decimal, then it'll also be digit and numeric.

Related: How to Create and Use Tuples in Python

Examples:

        # Digits
str1 = "8734947"
print("str1:")
print("str1.isdecimal() : ",str1.isdecimal())
print("str1.isdigit() : ",str1.isdigit())
print("str1.isnumeric() : ",str1.isnumeric())

# Fractional Value
str2 = "½"
print("str2:")
print("str2.isdecimal() : ",str2.isdecimal())
print("str2.isdigit() : ",str2.isdigit())
print("str2.isnumeric() : ",str2.isnumeric())

# Superscript Values
str3 = "⁰¹²³⁴⁵⁶⁷⁸⁹"
print("str3:")
print("str3.isdecimal() : ",str3.isdecimal())
print("str3.isdigit() : ",str3.isdigit())
print("str3.isnumeric() : ",str3.isnumeric())

Output:

        str1:
str1.isdecimal() : True
str1.isdigit() : True
str1.isnumeric() : True
str2:
str2.isdecimal() : False
str2.isdigit() : False
str2.isnumeric() : True

str3:
str3.isdecimal() : False
str3.isdigit() : True
str3.isnumeric() : True

How to Check if a String Is a Valid Identifier

You can check if the given string is a valid identifier using the isidentifier() method. This method returns True if the string is a valid identifier. Otherwise, it returns False.

A string is said to be a valid identifier if it satisfies the following conditions:

1. It only contains alphanumeric characters and/or underscores.

2. It doesn't start with a numeric character.

3. It doesn't contain any whitespaces.

Related: How to Use For Loops in Python

Examples:

        str1 = "var1"
# This method will return "True" as the given string is a valid identifier
checkstr1 = str1.isidentifier()
print(checkstr1)

str2 = "var 1"
# This method will return "False" as the string contains a whitespace
checkstr2 = str2.isidentifier()
print(checkstr2)

str3 = "var1#"
# This method will return "False" as the string have a special character "#"
checkstr3 = str3.isidentifier()
print(checkstr3)

str4 = "_var1_"
# This method will return "True" as the given string is a valid identifier
checkstr4 = str4.isidentifier()
print(checkstr4)

str5 = "1var"
# This method will return "False" as the string starts with a digit "1"
checkstr5 = str5.isidentifier()
print(checkstr5)

Output:

        True
False
False
True
False

How to Check if All Characters in a String Are Upper Case or Lower Case

You can check if the given string consists of only upper case characters using the isupper() method.

Similarly, you can check if the given string consists of only lower case characters using the islower() method. The isupper() method returns True if all characters of the string are in uppercase and the islower() method returns True if all characters of the string are in lowercase.

Examples:

        str1 = "MAKEUSEOF"
print("str1:")
print("str1.isupper() : ",str1.isupper())
print("str1.islower() : ",str1.islower())

str2 = "makeuseof"
print("str2:")
print("str2.isupper() : ",str2.isupper())
print("str2.islower() : ",str2.islower())

str3 = "MakeUseOf"
print("str3:")
print("str3.isupper() : ",str3.isupper())
print("str3.islower() : ",str3.islower())

Output:

        str1:
str1.isupper() : True
str1.islower() : False
str2:
str2.isupper() : False
str2.islower() : True
str3:
str3.isupper() : False
str3.islower() : False

How to Check if All Characters in a String Are Whitespace

You can check if the given string consists of only whitespace characters using the isspace() method.

This method returns True if all the characters are whitespace. If any of the characters in the string are not whitespace, this method returns False.

Examples:

        str1 = " "
# This method will return "True" as the given string contains only whitespace
checkstr1 = str1.isspace()
print(checkstr1)

str2 = " MUO "
# This method will return "False" as the string contains characters other than whitespace
checkstr2 = str2.isspace()
print(checkstr2)

str3 = " - "
# This method will return "False" as the string contains character other than whitespace
checkstr3 = str3.isspace()
print(checkstr3)

Output:

        True
False
False

How to Check if Text Follows Title Style Rules

You can check if the given text follows the rules of a title using the istitle() method.

A text is said to follow title rules if all the words in the text start with an upper case letter and the rest of the words are lower case letters. If the given text follows this rule, the istitle() method returns True, otherwise, it returns False.

Examples:

        str1 = "Welcome To Muo"
# This method will return "True" as the given string follows the rules of a title
checkstr1 = str1.istitle()
print(checkstr1)

str2 = "Welcome to Muo"
# This method will return "False" as the second word ("to") doesn't starts with a capital case character
checkstr2 = str2.istitle()
print(checkstr2)

str3 = "welcome to muo"
# This method will return "False" as all the words starts with a lowercase character
checkstr3 = str3.istitle()
print(checkstr3)

Output:

        True
False
False

Make Programming Fun Using Strings

Strings are very useful when communicating information from the program to its users. Using boolean validation isn't the only way to play around with Python strings, though.

You can convert boring text into interesting text by manipulating strings. Get creative and make programming fun!