[Tutor] Re: all the slashes
Abel Daniel
abli at freemail.hu
Mon Feb 9 13:41:14 EST 2004
Gerhard Venter writes:
> Hi all
>
> I use the code below in Python 2.3, but in 2.2 it says:
> TypeError: 'in <string>' requires character as left operand
> I have tried r'//', and '////' (to escape the slashes) to no avail.
>
> myfile=open(r'/etc/sitelist','a')
> if "//" in mystring:
>
Why would you need to escape it?
>>> '/' in 'ab/c'
True
>>> '/' in 'abc'
False
>>>
OTOH, if you would be looking for \ -s (backslashes) you would need to escape
it:
>>> '\' in 'ab\c'
File "<stdin>", line 1
'\' in 'ab\c'
^
SyntaxError: invalid syntax
>>> '\\' in 'ab\c'
True
>>>
Plus, if you do use the code you showed, then you have a bug: your version
is looking for '//' (two slashes) and not '/' (one slash).
--
Abel Daniel
More information about the Tutor
mailing list