Why does not this code work when I delete "r" in line 3? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Why does not this code work when I delete "r" in line 3?


24th Mar 2018, 12:34 AM
Vučko/Вучко/Vuchko - avatar
4 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'.
27th Mar 2018, 4:39 AM
Nikolay Nachev
Nikolay Nachev - avatar
+ 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 😃
27th Mar 2018, 7:04 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 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 ;)
27th Mar 2018, 6:23 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
example,please. r"g.y" and "g.y" matches "gXy" and "g.y", but not "gX/ny" or "g./ny"
27th Mar 2018, 6:44 PM