輸入一個字串,將字串中的特定片段用另一個字串取代,最後將字串反轉。
def replaceAndReverse(original,toReplace,replacement):
#字串取代
while toReplace in original:
original = original.replace(toReplace, replacement)
#反轉字串
reversed_string = original[::-1]
results = reversed_string
return results
#輸入
original = input("原始字串: ")
toReplace = input("被取代的字串: ")
replacement = input("取代用的字串: ")
#輸出
print(replaceAndReverse(original, toReplace, replacement))
input:
BA122DC12FE
12
31
output:
EF13CD133AB