[Tutor] searching for newlines does not work!

Alan Gauld alan.gauld at btinternet.com
Sun Oct 24 00:47:44 CEST 2010


"Adam Bark" <adam.jtm30 at gmail.com> wrote

>> >>> test=r"a \n b \n c \n"
>> >>> test.replace(r"\n","***")
>> 'a *** b *** c ***'
>>
>> >>> test2="""
>> ... a
>> ... b
>> ... c
>> ... """
>> >>> test2.replace(r"\n","***")
>> '\na\nb\nc\n'

> In your first example you put the characters into the string test 
> and replaced the characters.
> The second example you have actual newlines and are searching for 
> the characters "/n" which don't exist in your string.

Just to further emphasise this point try printing your strings (using 
print())
and you will see the difference:

>>> test = r"a \n b \n c \n"  # using raw 'r'
>>> test2 = """
... a
... b
... c
... """
>>> test3 = "a \n b \n c \n"  # no raw 'r'
>>> print(test)
a \n b \n c \n
>>> print( test2)

a
b
c

>>> print(test3)
a
 b
 c

>>>

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list