Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38806

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

Newsgroups comp.lang.python
Date 2013-02-12 20:44 -0800
References <mailman.1536.1360407866.2939.python-list@python.org>
Subject Re: string.replace doesn't removes ":"
From Rick Johnson <rantingrickjohnson@gmail.com>
Message-ID <mailman.1736.1360730657.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Saturday, February 9, 2013 5:04:18 AM UTC-6, 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 ?

Not a bug, you are just expecting Python to read your mind. In actuality what you are doing is asking the method "string.replace()" to replace the 32 char string:

   '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' 

with the null string: 

   ""

...what you INTENDED to do was have Python find every occurrence the each char in the string you passed and replace them with the null string. You are assuming the replace method handles sequences in the manner you are projecting. Sorry, but string#replace does not work that way. Whether or not it should is up for debate[1].

However, you can create a loop and pass the arguments "one-by-one" into the method:

py> s = "a:b  :c: d"
py> for char in string.punctuation:
	s = s.replace(char, "")	
py> s
'ab  c d'

But this seems really wasteful if all you want is to remove colons. Are you just removing the colons or truly wanting to remove ALL punctuation from the string?

============================================================
 REFERENCES:
============================================================

[1]: Should string.replace handle list, tuple and dict arguments in addition to strings?

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

This would be a more consistent approach to me. Handling only string arguments is woefully inadequate. Why would you have a programmer write a loop for this every time? 

@pydev&GvR
What is the justification for not processing (at the minimum) multiple arguments? Do you think strings will most often only need one modification?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

string.replace doesn't removes ":" Joshua Robinson <shooki.robinson@gmail.com> - 2013-02-09 06:04 -0500
  Re: string.replace doesn't removes ":" Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-02-10 11:36 +0100
    Re: string.replace doesn't removes ":" vduncan80@gmail.com - 2013-02-12 07:14 -0800
  Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800
    Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
    Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 21:26 -0800
      Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-12 23:10 -0800
        Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-13 08:34 -0800
          Re: string.replace doesn't removes ":" Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-13 16:55 +0000
            Re: string.replace doesn't removes ":" Walter Hurry <walterhurry@lavabit.com> - 2013-02-13 18:16 +0000
          Re: string.replace doesn't removes ":" 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-13 12:24 -0800
            Re: string.replace doesn't removes ":" jmfauth <wxjmfauth@gmail.com> - 2013-02-14 00:02 -0800
  Re: string.replace doesn't removes ":" Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 20:44 -0800

csiph-web