How to insert in a string @ a index

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Sep 10 03:39:21 EDT 2007


On Mon, 10 Sep 2007 00:29:50 -0700, Ant wrote:

> On Sep 10, 3:15 am, "a.m." <lolu... at gmail.com> wrote:
>> Thanks guys for you help. I ended up doing this way (for the
>> records)...
>>
>> t1 = "hello world hello. hello. \nwhy world hello"
> ...
> 
> Another approach may be to use the re module's sub function:
> 
> import re
> 
> t1 = 'hello world hello. hello. \nwhy world hello'
> 
> def matchHandler(match):
>     if <test here>:
>        return "XYZ" + match.group(0)
>     else:
>        return match.group(0)
> 
> re.sub(keyword, matchHandler, t1)

This doesn't work because according to the OP not all 'hello's are
keywords.  It seems to depend on the position if it's a keyword or not.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list