python - Insert a string before a substring of a string -
i want insert text before substring in string.
for example:
str = "thisissometextthatiwrote" substr = "text" inserttxt = "xx"
i want:
str = "thisissomexxtextthatiwrote"
assuming substr can appear once in str, how can achieve result? there simple way this?
my_str = "thisissometextthatiwrote" substr = "text" inserttxt = "xx" idx = my_str.index(substr) my_str = my_str[:idx] + inserttxt + my_str[idx:]
ps: avoid using reserved words (i.e. str
in case) variable names
Comments
Post a Comment