What does r"\w" mean and how do backslashes work in raw strings? Can I use another letter apart from w? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does r"\w" mean and how do backslashes work in raw strings? Can I use another letter apart from w?

I was trying to solve the code challenge for metacharacters and I wasn't getting it. Looking at the solution I saw that "\w" was used but I don't get it. In the previous classes, I just used r"[A-Z][0-9]" and it worked so I'm wondering why it's different now.

11th Feb 2023, 7:48 AM
Olamilekan Alexander Fasanya
4 Answers
+ 4
"[A-Z][0-9]" matches capital alphabet followed by a digit. \w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_]).
11th Feb 2023, 8:25 AM
Jayakrishna 🇮🇳
+ 2
Thank you for this Jayakrishna 🇮🇳 . If I can do it the way you have done in your example then what is the need for metacharacters like *,+ and ? ?
11th Feb 2023, 10:06 AM
Olamilekan Alexander Fasanya
+ 2
*, +,?, {m, n} are repetition characters. * for 0 or more repeated... + for 1 or more, ? 0 or 1 {m, n} atleast m, to n times at most.
11th Feb 2023, 10:32 AM
Jayakrishna 🇮🇳
+ 1
Ok, thank you sir.
11th Feb 2023, 4:24 PM
Olamilekan Alexander Fasanya