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?
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.