What are character class operations in Python?


Character class operations are a way to match certain types of characters in a string using regular expressions in Python.

In regular expressions, a "character class" is a set of characters that you want to match. You can use square brackets `[]` to create a character class. For example, if you want to match any vowel, you can use the character class `[aeiou]`. This will match any single character that is either 'a', 'e', 'i', 'o', or 'u'.

Example

This code will search for all the vowels in the `text` string using the regular expression `[aeiou]`, and it will return a list of all the vowels it finds.

As you can see, the output is a list of all the vowels in the string, in the order in which they appear.

import re
text = "The quick brown fox jumps over the lazy dog."
vowels = re.findall("[aeiou]", text)
print(vowels)

Output

['e', 'u', 'i', 'o', 'o', 'u', 'e', 'o', 'e', 'a', 'o']

Example

You can use the caret `^` character inside a character class to match any character that is not in the class. For example, the character class `[^aeiou]` will match any character that is not a vowel. Here's an example:

This code will search for all the consonants (i.e., any character that is not a vowel or a space) in the `text` string using the regular expression `[^aeiou ]`, and it will return a list of all the consonants it finds. The output will look like this:

As you can see, the output is a list of all the consonants in the string, in the order in which they appear.

import re
text = "The quick brown fox jumps over the lazy dog."
consonants = re.findall("[^aeiou ]", text)
print(consonants)

Output

['T', 'h', 'q', 'c', 'k', 'b', 'r', 'w', 'n', 'f', 'x', 'j', 'm', 'p', 's', 'v', 'r', 't', 'h', 'l', 'z', 'y', 'd', 'g', '.']

Example

You can use the hyphen `-` character to specify a range of characters inside a character class. For example, the character class `[A-Z]` will match any uppercase letter from A to Z. Here's an example:

This code will search for all the uppercase letters in the `text` string using the regular expression `[A-Z]`, and it will return a list of all the uppercase letters it finds.

As you can see, the output is a list that contains only the uppercase letter 'T' because it is the only uppercase letter in the string.

import re
text = "The quick brown fox jumps over the lazy dog."
capital_letters = re.findall("[A-Z]", text)
print(capital_letters)

Output

['T']

Example

You can combine character classes to match more complex patterns. For example, the regular expression `[\w\d]` will match any alphanumeric character. Here's an example:

This code will search for all the alphanumeric characters (i.e., any letter or digit) in the `text` string using the regular expression `[\w\d]`, and it will return a list of all the alphanumeric characters it finds.

As you can see, the output is a list of all the alphanumeric characters in the string, in the order in which they appear.

import re
text = "The price is $2.99."
alphanumeric = re.findall("[\w\d]", text)
print(alphanumeric)

Output

['T', 'h', 'e', 'p', 'r', 'i', 'c', 'e', 'i', 's', '2', '9', '9']

Example

You can use the backslash `\` character to escape special characters inside a character class. For example, the regular expression $'[\backslash[\backslash]]'$ will match any square bracket character (`[` or `]`). Here's an example:

This code will search for all the square bracket characters in the `text` string using the regular expression $'[\backslash[\backslash]]'$, and it will return a list of all the square bracket characters it finds. The output will look like this:

As you can see, the output is a list that contains both square bracket characters in the string because they are the only characters that match the regular expression $'[\backslash[\backslash]]'$.

import re
text = "This is a [test]."
brackets = re.findall("[\[\]]", text)
print(brackets)

Output

['[', ']']

These are just a few examples of how you can use character class operations in Python. By combining character classes and other regular expression operations, you can match almost any pattern you can think of in a text string.

Updated on: 19-May-2023

992 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements