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

Rate this post

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)
['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:

Python Re Dot - A Simple Tutorial

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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn 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