Using the Question Mark Wildcard
Let’s take a look at the next wildcard character, the
? (question mark). The
? matches any single character in a pattern. It matches any character. In that way it’s similar to the
*, but not any number of characters. Let’s take a look in IDLE.
And there’s another way that you could match them. You could say
"goals?.txt" because this is going match every file that starts with
goals then has one single character and then is followed by
But if you would have some notes about foals or some notes about coals, then these would match here as well, and with any character at the end. Just like the
* character, you can also use the
? character more than once in a pattern.
Consult your directory structure. And then here’s the solution. This is going to match the
README.md file because the first part could be anything, so the stem of the filename could be anything.
And in this case, it’s
README. And then after the
. you have exactly two characters, which is
.md. And neither
goals2.txt match on this because their file extension has three characters, and you limit it to two by using two
? wildcards after the dot.
So these are ways that you can use the
? wildcard in a pattern. It matches a single character in a pattern. We tried the example of saying
"goals?.txt", which matches both
Become a Member to join the conversation.