例如:
STR = “i am beautiful, and you?”
需求:
在 ‘beautiful’ 前边加入 ‘so’
STR = “i am beautiful, and you?”
# step1: 找到你想要插入位置的索引
str_index = STR.find('beautiful') # rfind 返回字符串最后一次出现的位置,如果没有匹配项则返回-1
# step2: 拼接插入
new_str = STR[:str_index] + ' so' + STR[str_index:]
print(new_str)