find and remove "\" character from string

  • Thread starter Konstantinos Pachopoulos
  • Start date
K

Konstantinos Pachopoulos

Hi,
i have the following string s and the following code, which doesn't
successfully remove the "\", but sucessfully removes the "\\".
.... if i!="\\":
.... newS=newS+i
....'Sadasd\x07sd'

I have also read the following, but i do not understand the "...and the
remaining characters have been mapped through the given translation
table, which must be a string of length 256". Can some explain?

*translate*( table[, deletechars])

Return a copy of the string where all characters occurring in the
optional argument deletechars are removed, and the remaining
characters have been mapped through the given translation table,
which must be a string of length 256.

For Unicode objects, the translate() method does not accept the
optional deletechars argument. Instead, it returns a copy of the s
where all characters have been mapped through the given translation
table which must be a mapping of Unicode ordinals to Unicode
ordinals, Unicode strings or |None|. Unmapped characters are left
untouched. Characters mapped to |None| are deleted. Note, a more
flexible approach is to create a custom character mapping codec
using the codecs <http://docs.python.org/lib/module-codecs.html>
module (see encodings.cp1251 for an example).
 
S

Stefan Behnel

Konstantinos said:
i have the following string s and the following code, which doesn't
successfully remove the "\", but sucessfully removes the "\\".

... if i!="\\":
... newS=newS+i

I'm not quite sure what you're trying to achieve, but I'd use
'ab\\c'
'abc'

Note that "\\" in the source is unescaped to "\" in the string. Use r"\\" to
prevent that.

Stefan
 
I

I V

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.

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.
... if i!="\\":

Here, your test is true if i is not \
... newS=newS+i
...
'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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top