How to Implement Wildcards in Python

Wildcards in Python are used to represent one or many characters. There are two wildcard characters in Python; * (asterisk) which represents one or more characters and ? (question mark) which represents a single character.

 

Using Wildcards

To implement wildcards in Python, we'll need to import a regular expression package as Python doesn't have this functionality out of the box. The best option is to import re and use the re.search() function for pattern matching.

 

import re

words = 'Here are some words.'

result = re.search(' .*', words)

print(result.group(0))
are some words.

 

In the above example, we get everything in the string starting from the first space character.