根据引号分隔符对文本进行切片,并仅处理位置为 pair 的子字符串。
string = '''Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of lorem ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)'''
l = string.split('"')
new_l = []
for position,substring in list(enumerate(l)):
if position%2 == 0:
s = substring.replace('lorem','STACK OVERFLOW')
new_l.append(s)
else:
new_l.append(substring)
new_string = '"'.join(new_l)
print new_string
输出 :
Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use STACK OVERFLOW ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of STACK OVERFLOW ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)
第一个“lorem”被忽略,因为它在引用中。