+ 9
Why does not this code work when I delete "r" in line 3?
5 Answers
+ 15
the r stands for Regular Expression. Once you remove it you are looking for a substring 'g.y' which is not found in your cases. With the r you are matching a regex that searches for a substring that matches the pattern letter 'g' followed by any character, followed by the letter 'y'.
+ 15
@Kuba - now when I think about it it makes perfect sense š¤ wonder where I got that from. Of course that makes my previous post a complete rubbish š
+ 9
Nikolay Actually, r stands for "raw string literal", that is, the exact form of the string is represented by it - no escape characters, no special characters, just raw, exact (byte) representation.
The 're' module uses rawstrings for patterns as they are the safest way to go - regex have to take string literals and not to worry about what the \n means ;)
+ 1
example,please.
r"g.y" and "g.y" matches "gXy" and "g.y", but not "gX/ny" or "g./ny"