find and remove "\" character from string

Discussion in 'Python' started by Konstantinos Pachopoulos, Sep 15, 2007.

  1. 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'

    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).
    Konstantinos Pachopoulos, Sep 15, 2007
    #1
    1. Advertising

  2. Konstantinos Pachopoulos wrote:
    > 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


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

    >>> r"\\a\\b\c".replace("\\\\", "")

    'ab\\c'

    >>> r"\\a\\b\c".replace("\\", "")

    'abc'

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

    Stefan
    Stefan Behnel, Sep 15, 2007
    #2
    1. Advertising

  3. Konstantinos Pachopoulos

    I V Guest

    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'.
    I V, Sep 15, 2007
    #3
    1. Advertising

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
    Replies:
    18
    Views:
    14,624
    brian
    Apr 10, 2004
  2. Daniel Mark
    Replies:
    6
    Views:
    70,715
    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
    Replies:
    8
    Views:
    715
    those who know me have no need of my name
    May 28, 2006
  4. Steve Holden
    Replies:
    0
    Views:
    517
    Steve Holden
    Sep 15, 2007
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    704
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page