split string into multi-character "letters"

Terry Reedy tjreedy at udel.edu
Wed Aug 25 20:15:31 EDT 2010


> On 08/25/10 14:46, Jed wrote:

>> I would like to split the string 'churro' into a list containing:
>>
>> 'ch','u','rr','o'

Dirt simple, straightforward, easily generalized solution:

def sp_split(s):
     n,i,ret = len(s), 0, []
     while i < n:
         s2 = s[i:i+2]
         if s2 in ('ch', 'll', 'rr'):
             ret.append(s2)
             i += 2
         else:
             ret.append(s[i])
             i += 1
     return ret

print(sp_split('churro'))

#'ch', 'u', 'rr', 'o']
-- 
Terry Jan Reedy




More information about the Python-list mailing list