string.replace doesn't removes ":"

Rick Johnson rantingrickjohnson at
Wed Feb 13 06:26:37 CET 2013

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

> ============================================================
> ============================================================
> [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'), "")
py> 'abcdefgabc'.replace(['a', 'b', 'c'], "")
py> 'abcdefgabc'.replace({'a':'A', 'b':'2', 'c':'C'})

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())

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