Python手动实现字符串替换功能的代码补全
题目描述
小明在学习Python后尝试实现类似Word的字符串替换功能:在字符串s中,查找指定子串c1(可以是单个或多个字符)并全部替换为c2。
示例:输入s为"A person in need is a person indeed!",c1为person,c2为"friend",输出结果为"A friend in need is a friend indeed!"。
给定如下Python代码框架,请补全划线处的代码:
s = input("请输入文本字符串:")
c1 = input("请输入要替换的字符:")
c2 = input("请输入要替换成的新字符:")
n = len(c1)
s1 = ""
i = 0
while i < len(s):
c = ①
if c == c1:
s1 = s1 + c2
i = ②
else:
s1 = ③
④
print("替换后的结果:", s1) 本题为编程题,提交代码并运行通过测试点后判分。
正在加载题目数据…
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析