For teaching purposes, I would like a good explanation and example of why the "\" is needed in searching for, say a period or a character that python may interpret differently.
In the below example, I do NOT use a "\" to escape the "." and it still works fine.
def checkfordot():
dotpattern="[.]"
searchingfor=input("Enter string:")
if (re.search(dotpattern,searchingfor)):
print("Has a dot")
else:
print("Does not have a dot")
Output:
>>> checkfordot()
Enter string:dfsdf.
Has a dot
>>> checkfordot()
Enter string:ffsdfsdfsdf
Does not have a dot
>>>
In this case, using dotpattern="[.]" or dotpattern="[.]" doesn't seem to make much difference.
Can someone a) explain why the above works without an escape and a simple explanation of what the escape does and in what situations b) provide an example or ideally two examples, integrated into my code, that demonstrates well why the escape character is required and how it is used for effective searching (or vice versa, in that without the "" the program would not work)
Ironically, in typing this out - I noticed I had to escape the \ in order for it to be printed. Here I am typing: >: " \ \ " ....but it prints just one backslash >"\"