string.replace doesn't removes ":"

jmfauth wxjmfauth at gmail.com
Thu Feb 14 09:02:10 CET 2013


On 13 fév, 21:24, 88888 Dihedral <dihedral88... at googlemail.com> 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





More information about the Python-list mailing list