Short question on raw strings

Hi all, I just have a short question on the Password Generator course.

Import re
pattern = r'\.'
quote = 'Not all those who wander are lost.'
print(re.findall(pattern, quote))

The output is " . "

However, doesn’t “r” treat the backlash as a literal character instead of escape character? Why is the dot character still escaped to become a full stop?

Grateful for your help, thanks!

In regex literal dot . is special character meaning any character. Without escaping it in the pattern, all characters in string would be matched:

>>> re.findall('.', 'Not all those who wander are lost.')
['N', 'o', 't', ' ', 'a', 'l', 'l', ' ', 't', 'h', 'o', 's', 'e', ' ', 'w', 'h', 'o', ' ', 'w', 'a', 'n', 'd', 'e', 'r', ' ', 'a', 'r', 'e', ' ', 'l', 'o', 's', 't', '.']
pattern = r'\.'

@sanity Yup, but doesn’t the raw string “r” negate the backlash such that escaping doesn’t occur? Why was the dot still escaped?

Uhm, not exactly, part of issue might be term escaping in two slightly different places. Dot is being escaped in the pattern (for the pattern), not in the string itself, as it doesn’t have any special meaning in strings.

r'\.' is nothing more than backslash followed by dot. Only in context of regex this dot is escaped.

>>> print(r'\.')
\.
>>> print('\.')
\.

Ie. for \n this is different:

>>> print(r'\n')
\n
>>> print('\n')


Technically r'\n' is equal to \\n. In which backslash is escaped, so it appears as literal \, and not a part of new line character - \n.

1 Like