string.replace doesn't removes ":"

Rick Johnson rantingrickjohnson at gmail.com
Wed Feb 13 00:26:37 EST 2013


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) 



More information about the Python-list mailing list