python编程题求解

寻找字符串"I think hope is a good thing."中字符串"hope"的下标,并将字符串中的hope替换成freedom,将新字符串保存到变量result中并在屏幕上输出。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author: YangPC
@time:2021/06/17
@QQ:327844761
@微信公众号:ewbang
"""


def fun():
    s = "I think hope is a good thing."
    t = "hope"
    index = s.index(t)
    print(f"下标:{index}-{index + len(t)}")
    # 将字符串中的hope替换成freedom
    result = s.replace("hope", 'freedom')
    print(result)


if __name__ == '__main__':
    fun()

程序输出结果:编码不易,望采纳!