第12375题 程序题
Python手动实现字符串替换功能的代码补全

题目描述

小明在学习Python后尝试实现类似Word的字符串替换功能:在字符串s中,查找指定子串c1(可以是单个或多个字符)并全部替换为c2。 示例:输入s"A person in need is a person indeed!"c1personc2"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)
程序运行统计
暂无判题统计