find and remove "\" character from string

I V ivlenin at gmail.com
Sat Sep 15 16:33:27 EDT 2007


On Sat, 15 Sep 2007 19:34:45 +0300, Konstantinos Pachopoulos wrote:
> Hi,
> i have the following string s and the following code, which doesn't
> successfully remove the "\", but sucessfully removes the "\\".

There is no \\ in the string; there's one \ , which gets succesfully 
removed.

>  >>> s="Sad\\asd\asd"

When you write a string in the source code \\ gets changed to \ and \a 
gets changed to "ASCII Bell (BEL)" (that's what the docs say), which is a 
(non-printable) control code that is supposed to make the terminal beep.

>  >>> newS=""
>  >>> for i in s:
> ...     if i!="\\":

Here, your test is true if i is not \

> ...             newS=newS+i
> ...
>  >>> newS
> 'Sadasd\x07sd'

And here, you have a string containing no backslashes, but containing a 
character with ASCII code 7; it turns out that ASCII code 7 is the "ASCII 
Bell", i.e., the character that you added to the string when you wrote 
'\a'.



More information about the Python-list mailing list