How to implement wildcards in Python

A wildcard is a symbol used to replace or represent one or more characters. Wildcards are used in computer programs, languages, search engines, and operating systems to simplify search criteria. The most common wildcards are the asterisk * and the question mark ??.


Types of wildcards

The asterisk (*)

An asterisk * is used to specify any number of characters. It is typically used at the end of a root word. This is great when you want to search for variable endings of a root word.

For example, searching for work* would tell the database to look for all possible word-endings to the root “work”. Results will include “working” and “worker”, among others depending on our list of words.

svg viewer

The question mark (??)

A question mark ?? is used to represent a single character, anywhere in the word. It is most useful when there are variable spellings for a word, and you want to search for all variants at once.

For example, searching for col?r would return “color”.


Implementation

In Python, we can implement wildcards using the regex (regular expressions) library.

The dot . character is used in place of the question mark ?? symbol. Hence,​ to search for all words matching the col?r pattern, the code would look something like as follows.

# Regular expression library
import re
# Add or remove the words in this list to vary the results
wordlist = ["color", "colour", "work", "working",
"fox", "worker", "working"]
for word in wordlist:
# The . symbol is used in place of ? symbol
if re.search('col.r', word) :
print (word)

Similarly, the .+ characters are used to match one or more characters (like the asterisk * symbol). Hence, in Python, to search for all words starting with the root “work”, our regex code would look like this:

# Regular expression library
import re
# Add or remove the words in this list to vary the results
wordlist = ["color", "colour", "work", "working",
"fox", "worker"]
for word in wordlist:
# The .+ symbol is used in place of * symbol
if re.search('work.+', word) :
print (word)

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved