How to insert in a string @ a index

Zentrader zentraders at gmail.com
Sat Sep 8 17:47:20 EDT 2007


Same solution as above, but if you just want "Hello" and to not
include words containing "Hello", i.e. "Helloing" or "Unhello", then
you want to include a leading and/or trailing space.

lit=" hello"  ## note added space
t1="nothello world hello. hello \nwhy world hello"
start = t1.find(lit)
t2 = t1[:start+1] + " XYZ" + t1[start+1:]
print "t1 =", t1
print "t2 =", t2
start = t2.rfind(lit)
t3 = t2[:start+1] + " XYZ" + t2[start+1:]
print "t3 =", t3




More information about the Python-list mailing list