Re: find and remove "\" character from string

Discussion in 'Python' started by Steve Holden, Sep 15, 2007.

  1. Steve Holden

    Steve Holden Guest

    Konstantinos Pachopoulos wrote:
    > Hi,
    > i have the following string s and the following code, which doesn't
    > successfully remove the "\", but sucessfully removes the "\\".
    > >>> s="Sad\\asd\asd"
    > >>> newS=""
    > >>> for i in s:

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

    > 'Sadasd\x07sd'

    In actual fact there was just a single backslash in s to start with. If
    you read the documentation carefully at

    (though it's the language reference manual, and therefore not
    necessarily suitable reading for beginners) you will see that the "\\"
    represents a single backslash character and \a represents an ASCII BEL
    character (whose decimal value is 7, and which the interpreter
    represents as the hexadecimal escape string \x07).

    So the characters in s were S a d \ a s d \x07 s d and you shoudl have
    seen len(s) == 10.

    As has already been mentioned, the shortest way to do what you want would be

    newS = s.replace("\\", "")

    > 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 <>
    > module (see encodings.cp1251 for an example).

    The translate() string method uses the numeric represetation of each
    character as an index into the translation table. So a null translation
    table can be constructed using

    >>> t = "".join(chr(i) for i in range(256))
    >>> t


    (the above output will look a little screwy in the mail because of odd
    line wrapping).

    So hopefully you could then achieve the same effect (at vastly greater
    complexity than the first solution) using

    >>> s="Sad\\asd\asd"
    >>> len(s)

    >>> newS = s.translate(t, "\\")
    >>> newS


    You would probably only want to use that method if you were actually
    translating some of the characters at the same time.

    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd
    Skype: holdenweb

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 15, 2007
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toto

    how to remove a character from a string

    Toto, Apr 7, 2004, in forum: C Programming
    Apr 10, 2004
  2. Daniel Mark
    Tim Chase
    Sep 28, 2006
  3. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    those who know me have no need of my name
    May 28, 2006
  4. Konstantinos Pachopoulos

    find and remove "\" character from string

    Konstantinos Pachopoulos, Sep 15, 2007, in forum: Python
  5. Bart Vandewoestyne
    Bart Vandewoestyne
    Sep 25, 2012

Share This Page