Le 22/10/2016 à 10:34, Simon Mark Holland a écrit :
Having researched this as heavily as I am capable with limited
experience, I would like to suggest a Python 3 equivalent to
string.translate() that doesn't require a table as input.  Maybe in the
form of str.stripall() or str.replaceall().

My reasoning is that while it is currently possible to easily strip()
preceding and trailing characters, and even replace() individual
characters from a string, to replace more than one characters from
anywhere within the string requires (i believe) at its simplest a
command like this :

some_string.translate(str.maketrans('','','0123456789'))

In Python 2.* however we could say ...

some_string.translate(None, '0123456789')

My proposal is that if strip() and replace() are important enough to
receive modules, then the arguably more common operation (in terms of
programming tutorials, if not mainstream development) of just removing
all instances of specified numbers, punctuation, or even letters etc
from a list of characters should also.

I wholeheartedly admit that there are MANY other ways to do this
(including RegEx and List Comprehensions), as listed in the
StackOverflow answer below.   However the same could be said for
replace() and strip().

This actually could be implemented directly in str.replace() without breaking the API by accepting:

"stuff".replace('a', '')
"stuff".replace(('a', 'b', 'c'), '')
"stuff".replace(('a', 'b', 'c'), ('?', '*', ''))

A pure Python implementation looks like this:

https://github.com/Tygs/ww/blob/dev/src/ww/wrappers/strings.py#L229

(this implementation also allow regexes, which is not what you want for the builtin replace(), however, as it would break the performances expectations)

I often had the use case of needing to strip many strings so I would +1 for having a nice and easy way to do it.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to