Python练习3-Eric
lists=[ "coaolvajcq_oldboy.html",
"gmkhrancxh_oldboy.html",
"jdxexendbe_oldboy.html",
"qcawgsrtkp_oldboy.html",
"qnvuxvicni_oldboy.html",
"tmdjormaxr_oldboy.html",
"ugaywanjlm_oldboy.html",
"vfrphtqjpc_oldboy.html",
"vioesjmcbu_oldboy.html",
"wzewnojiwe_oldboy.html",
"xzzruhdzda_oldboy.html"]
#第1种
print("这是第一种方法:")
change="oldgirl.HTML"
for i in range(len(lists)): #遍历列表元素,得到字符串
s=lists
n=s.find("oldboy") #find找到oldboy对应的开始下标
temp=s+change #利用上述下标,切片字符串前部分,并拼接赋值给temp
s=temp
print(s)
#第2种
print("这是第二种方法:")
change="oldgirl.HTML"
for i in range(len(lists)): #遍历列表元素,得到字符串
s=lists+change #直接在字符串后拼接
n=s.find("oldboy.html") #find找到oldboy对应的开始下标
m=n+len("oldboy.html") #上述下标加上change的长度为结束下标
temp=s+s #分片赋值给temp
s=temp
print(s)
页:
[1]