Does the Dot Regex (.) Match Whitespace Characters in Python?

Yes, the dot regex matches whitespace characters when using Python’s re module.

Consider the following example:

import re


string = 'The Dot Regex Matches Whitespace Characters'
match = re.findall('.', string)
print(match)
'''
['T', 'h', 'e', ' ',
'D', 'o', 't', ' ',
'R', 'e', 'g', 'e', 'x', ' ',
'M', 'a', 't', 'c', 'h', 'e', 's', ' ',
'W', 'h', 'i', 't', 'e', 's', 'p', 'a', 'c', 'e', ' ',
'C', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's']
'''

Try it yourself in our interactive Python shell (click “run”):

The dot matches all characters in the string–including whitespaces. You can see that there are many whitespace characters ' ' among the matched characters.

Need more info? Watch the simple tutorial video about the dot regex if you need more clarifications:

Note that the dot matches whitespace characters in all other regular expression languages I have found on the web—no matter the programming language or framework.

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Related articles:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Leave a Comment

Your email address will not be published. Required fields are marked *