[Python-ideas] str.find() and friends support a lists of inputs
Terry Reedy
tjreedy at udel.edu
Fri Apr 18 03:31:24 CEST 2014
On 4/17/2014 2:52 PM, Alex Rodrigues wrote:
> It's a fairly common problem to want to .find() or .replace() or .split() any one of multiple characters.
> Currently the go to solution are:
For replace, you left out the actual solution.
>>> telnum = '(800), 555-1234'
>>> telnum.translate(str.maketrans('','','(- ,)'))
'8005551234'
For finding any of multiple chars, a loop that does just what you want
is easy.
targets = '(- ,)'
for i, c in enumerate(s):
if c in targets:
break
else:
<whatever you want for not found>
<at this point, c and i are the char found and its index
A medium weight split is harder. A feature of re.split, because here are
multiple possible split strings, is that one can delete or keep the
split substring.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list