insert chars into string

Diez B. Roggisch deets at nospam.web.de
Sat Mar 18 13:44:30 EST 2006


eight02645999 at yahoo.com schrieb:
> hi
> is there a string method to insert characters into a string?
> eg
> str = "abcdef"
> i want to insert "#" into str so that it appears "abc#def"
> 
> currently what i did is convert it to a list, insert the character
> using insert() and then join them back as string.. 

If you are frequently want to modify a string in such a way, that this 
is the recommended way to do so. It might be that there are 
mutable-string implementations out there that make that easier for you - 
but in the end, it boils down to using list, so that the overhead of 
concatenating strings is encountered only once, on the end when you 
actually need the result.

Diez




More information about the Python-list mailing list