string.replace doesn't removes ":"

Discussion in 'Python' started by Joshua Robinson, Feb 9, 2013.

  1. Hi *Monte-Pythons*,

    x = "this is a simple : text: that has colon"
    s = x.replace(string.punctuation, ""); OR
    s = x.replace(string.punctuation, "");
    print x # 'this is a simple : text: that has colon'
    # The colon is still in the text !!!!

    Is this a bug or am I doing something wrong ?

    Py.Version: 2.7
    OS: Ubuntu 12.10 (64 bits)

    Cheers,
    -Joshua
    Joshua Robinson, Feb 9, 2013
    #1
    1. Advertising

  2. On 09.02.2013 12:04, Joshua Robinson wrote:
    > Hi *Monte-Pythons*,
    >
    > x = "this is a simple : text: that has colon"
    > s = x.replace(string.punctuation, ""); OR
    > s = x.replace(string.punctuation, "");
    > print x # 'this is a simple : text: that has colon'
    > # The colon is still in the text !!!!
    >
    > Is this a bug or am I doing something wrong ?


    The latter. str.replace() only replaces complete substrings, not single
    character occurences of the given pattern. That is

    "foo".replace("foo", "bar") == "bar"
    "foofoo".replace("foo", "bar") == "barbar"
    "foofoo".replace("fo", "bar") == "barobaro"
    "foofoo".replace("abcdef", "bar") == "foofoo"

    Regards,
    Johannes

    --
    >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

    > Zumindest nicht öffentlich!

    Ah, der neueste und bis heute genialste Streich unsere großen
    Kosmologen: Die Geheim-Vorhersage.
    - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>
    Johannes Bauer, Feb 10, 2013
    #2
    1. Advertising

  3. Joshua Robinson

    Guest

    On Sunday, February 10, 2013 4:36:53 AM UTC-6, Johannes Bauer wrote:
    > On 09.02.2013 12:04, Joshua Robinson wrote:
    >
    > > Hi *Monte-Pythons*,

    >
    > >

    >
    > > x = "this is a simple : text: that has colon"

    >
    > > s = x.replace(string.punctuation, ""); OR

    >
    > > s = x.replace(string.punctuation, "");

    >
    > > print x # 'this is a simple : text: that has colon'

    >
    > > # The colon is still in the text !!!!

    >
    > >

    >
    > > Is this a bug or am I doing something wrong ?

    >
    >
    >
    > The latter. str.replace() only replaces complete substrings, not single
    >
    > character occurences of the given pattern. That is
    >
    >
    >
    > "foo".replace("foo", "bar") == "bar"
    >
    > "foofoo".replace("foo", "bar") == "barbar"
    >
    > "foofoo".replace("fo", "bar") == "barobaro"
    >
    > "foofoo".replace("abcdef", "bar") == "foofoo"
    >
    >
    >
    > Regards,
    >
    > Johannes
    >
    >
    >
    > --
    >
    > >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

    >
    > > Zumindest nicht öffentlich!

    >
    > Ah, der neueste und bis heute genialste Streich unsere großen
    >
    > Kosmologen: Die Geheim-Vorhersage.
    >
    > - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>


    Hello Joshua:

    Hopefully you have worked out the issue. Johannes is right on the money using 'replace' as shown below.

    x = "this is a simple : text: that has colon
    s = x.replace(":", "")
    print(s)
    'this is a simple text that has colon'

    Sincerely,
    VDuncan
    , Feb 12, 2013
    #3
  4. Joshua Robinson

    Rick Johnson Guest

    On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote:

    > ============================================================
    > REFERENCES:
    > ============================================================
    > [1]: Should string.replace handle list, tuple and dict
    > arguments in addition to strings?
    >
    > py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
    > 'defg'
    > [...]


    And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

    Hypothetical Examples:

    py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
    'defg'
    py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
    'defg'
    py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
    'A2CdefgA2C'

    Or, an alternative to passing dict where both old and new arguments accept the sequence:

    py> d = {'a':'A', 'b':'2', 'c':'C'}
    py> 'abcdefgabc'.replace(d.keys(), d.values())
    'A2CdefgA2C'

    Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

    (hopefully there's no mistakes this time)
    Rick Johnson, Feb 13, 2013
    #4
  5. Joshua Robinson

    Rick Johnson Guest

    On Tuesday, February 12, 2013 10:44:09 PM UTC-6, Rick Johnson wrote:

    > ============================================================
    > REFERENCES:
    > ============================================================
    > [1]: Should string.replace handle list, tuple and dict
    > arguments in addition to strings?
    >
    > py> string.replace(('a', 'b', 'c'), 'abcdefgabc')
    > 'defg'
    > [...]


    And here is a fine example of how a "global function architecture" can seriously warp your mind! Let me try that again!

    Hypothetical Examples:

    py> 'abcdefgabc'.replace(('a', 'b', 'c'), "")
    'defg'
    py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
    'defg'
    py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})
    'A2CdefgA2C'

    Or, an alternative to passing dict where both old and new arguments accept the sequence:

    py> d = {'a':'A', 'b':'2', 'c':'C'}
    py> 'abcdefgabc'.replace(d.keys(), d.values())
    'A2CdefgA2C'

    Nice thing about dict is you can control both sub-string and replacement-string on a case-by-case basis. But there is going to be a need to apply a single replacement string to a sequence of substrings; like the null string example provided by the OP.

    (hopefully there's no mistakes this time)
    Rick Johnson, Feb 13, 2013
    #5
  6. Joshua Robinson

    Rick Johnson Guest

    On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
    >
    > >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
    > >>> 'abcdefgabc'.translate(d)

    > 'A2CdefgA2C'
    > >>>
    > >>>
    > >>> def jmTranslate(s, table):

    > ... table = {ord(k):table[k] for k in table}
    > ... return s.translate(table)
    > ...
    > >>> d = {'a': 'A', 'b': '2', 'c': 'C'}
    > >>> jmTranslate('abcdefgabc', d)

    > 'A2CdefgA2C'
    > >>> d = {'a': None, 'b': None, 'c': None}
    > >>> jmTranslate('abcdefgabc', d)

    > 'defg'
    > >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
    > >>> jmTranslate('abcdefgabc', d)

    > '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'


    [quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!

    "Beautiful is better than ugly."
    BROKEN!

    "Explicit is better than implicit."
    BROKEN!

    "Simple is better than complex."
    BROKEN!

    "Sparse is better than dense."
    BROKEN!

    "Readability counts."
    BROKEN BROKEN BROKEN!!!!

    "Special cases aren't special enough to break the rules."
    BROKEN!

    "In the face of ambiguity, refuse the temptation to guess."
    BROKEN!

    "There should be one-- and preferably only one --obvious way to do it."
    BROKEN BROKEN BROKEN!

    "If the implementation is hard to explain, it's a bad idea."
    BROKEN!

    "If the implementation is easy to explain, it may be a good idea."
    REINFORCED BY BAD EXAMPLE
    Rick Johnson, Feb 13, 2013
    #6
  7. On 13/02/2013 16:34, Rick Johnson wrote:
    > On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
    >>
    >>>>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}
    >>>>> 'abcdefgabc'.translate(d)

    >> 'A2CdefgA2C'
    >>>>>
    >>>>>
    >>>>> def jmTranslate(s, table):

    >> ... table = {ord(k):table[k] for k in table}
    >> ... return s.translate(table)
    >> ...
    >>>>> d = {'a': 'A', 'b': '2', 'c': 'C'}
    >>>>> jmTranslate('abcdefgabc', d)

    >> 'A2CdefgA2C'
    >>>>> d = {'a': None, 'b': None, 'c': None}
    >>>>> jmTranslate('abcdefgabc', d)

    >> 'defg'
    >>>>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}
    >>>>> jmTranslate('abcdefgabc', d)

    >> '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

    >
    > [quip] I just really prefer a cryptic solution to a problem when a simplistic and consistent approach would suffice.[/quip] TO HELL WITH THE ZEN!
    >
    > "Beautiful is better than ugly."
    > BROKEN!
    >
    > "Explicit is better than implicit."
    > BROKEN!
    >
    > "Simple is better than complex."
    > BROKEN!
    >
    > "Sparse is better than dense."
    > BROKEN!
    >
    > "Readability counts."
    > BROKEN BROKEN BROKEN!!!!
    >
    > "Special cases aren't special enough to break the rules."
    > BROKEN!
    >
    > "In the face of ambiguity, refuse the temptation to guess."
    > BROKEN!
    >
    > "There should be one-- and preferably only one --obvious way to do it."
    > BROKEN BROKEN BROKEN!
    >
    > "If the implementation is hard to explain, it's a bad idea."
    > BROKEN!
    >
    > "If the implementation is easy to explain, it may be a good idea."
    > REINFORCED BY BAD EXAMPLE
    >


    jmf and rr in combination reminded me of this. I hope you all get my
    drift :)

    http://www.cc.gatech.edu/fac/Spencer.Rugaber/poems/love.txt

    --
    Cheers.

    Mark Lawrence
    Mark Lawrence, Feb 13, 2013
    #7
  8. Rick Johnsonæ–¼ 2013å¹´2月14日星期四UTC+8上åˆ12時34分11秒寫é“:
    > On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:
    >
    > >

    >
    > > >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}

    >
    > > >>> 'abcdefgabc'.translate(d)

    >
    > > 'A2CdefgA2C'

    >
    > > >>>

    >
    > > >>>

    >
    > > >>> def jmTranslate(s, table):

    >
    > > ... table = {ord(k):table[k] for k in table}

    >
    > > ... return s.translate(table)

    >
    > > ...

    >
    > > >>> d = {'a': 'A', 'b': '2', 'c': 'C'}

    >
    > > >>> jmTranslate('abcdefgabc', d)

    >
    > > 'A2CdefgA2C'

    >
    > > >>> d = {'a': None, 'b': None, 'c': None}

    >
    > > >>> jmTranslate('abcdefgabc', d)

    >
    > > 'defg'

    >
    > > >>> d = {'a': '€€€€€', 'b': '€€€€', 'c': '€€€€'}

    >
    > > >>> jmTranslate('abcdefgabc', d)

    >
    > > '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

    >

    In python the variables of value types, and the variables of lists and
    dictionaries are passed to functions somewhat different.

    This should be noticed by any serious programmer in python.
    88888 Dihedral, Feb 13, 2013
    #8
  9. Joshua Robinson

    jmfauth Guest

    On 13 fév, 21:24, 88888 Dihedral <> wrote:
    > Rick Johnsonæ–¼ 2013å¹´2月14日星期四UTC+8上åˆ12時34分11秒寫é“:
    >
    >
    >
    >
    >
    >
    >
    > > On Wednesday, February 13, 2013 1:10:14 AM UTC-6, jmfauth wrote:

    >
    > > > >>> d = {ord('a'): 'A', ord('b'): '2', ord('c'): 'C'}

    >
    > > > >>> 'abcdefgabc'.translate(d)

    >
    > > > 'A2CdefgA2C'

    >
    > > > >>> def jmTranslate(s, table):

    >
    > > > ...     table = {ord(k):table[k] for k in table}

    >
    > > > ...     return s.translate(table)

    >
    > > > ...

    >
    > > > >>> d = {'a': 'A', 'b': '2', 'c': 'C'}

    >
    > > > >>> jmTranslate('abcdefgabc', d)

    >
    > > > 'A2CdefgA2C'

    >
    > > > >>> d = {'a': None, 'b': None, 'c': None}

    >
    > > > >>> jmTranslate('abcdefgabc', d)

    >
    > > > 'defg'

    >
    > > > >>> d = {'a': '€€€€€', 'b':'€€€€', 'c': '€€€€'}

    >
    > > > >>> jmTranslate('abcdefgabc', d)

    >
    > > > '€€€€€€€€€€€€€defg€€€€€€€€€€€€€'

    >
    > In python the variables of value types, and the variables of lists and
    > dictionaries are passed to functions somewhat different.
    >
    > This should be noticed by any serious programmer in python.


    ---------

    The purpose of my quick and dirty fct was to
    show it's possible to create a text replacement
    fct which is using exclusively text / strings
    via a dict. (Even if in my exemple, I'm using
    - and can use - None as an empty string !)


    You are right.

    It is also arguable, that beeing forced to have
    to use a number in order to replace a character,
    may not be a so good idea.

    This should be noticed by any serious language designer.

    More seriously.
    ..translate() is a very nice and underestimated method.

    jmf
    jmfauth, Feb 14, 2013
    #9
    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. Ronald Colijn
    Replies:
    1
    Views:
    533
    Martin Eklund
    Nov 27, 2003
  2. Ethan Furman
    Replies:
    3
    Views:
    246
    Roel Schroeven
    Jun 17, 2008
  3. Terry Reedy
    Replies:
    4
    Views:
    360
    Terry Reedy
    Jun 17, 2008
  4. Beznas
    Replies:
    8
    Views:
    180
    Evertjan.
    Sep 10, 2003
  5. Chris Angelico

    Re: string.replace doesn't removes ":"

    Chris Angelico, Feb 9, 2013, in forum: Python
    Replies:
    0
    Views:
    71
    Chris Angelico
    Feb 9, 2013
Loading...

Share This Page