my_str = "thisissometextthatiwrote" substr = "text" inserttxt = "XX" idx = my_str.index(substr) my_str = my_str[:idx] + inserttxt + my_str[idx:]