# How do backslashes work in Python Regular Expressions?

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

According to Python docs, perhaps the most important metacharacter in regular expressions is the backslash, \. As in Python string literals, the backslash can be followed by various characters to indicate various special sequences. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a [ or \, you can precede them with a backslash to remove their special meaning: \[ or \.

The following code highlights the function of backslash in Python regex

## Example

import re
result = re.search('\d', '\d')
print result
result = re.search(r'\d', '\d')
print result.group()

## Output

This gives the output

None
\d
Updated on 20-Feb-2020 05:54:55