python字符串:字符串的查找和替换
hello_str = "hello world"# 判断是否以指定字符串开始print(hello_str.startswith("he"))# 判断是否以指定字符串结尾print(hello_str.endswith("ld"))# 查找指定字符串# 返回索引print(hello_str.find("llo"))"""index也可以查找指定字符串find和index区别:find在找不到指
·
hello_str = "hello world"
# 判断是否以指定字符串开始
print(hello_str.startswith("he"))
# 判断是否以指定字符串结尾
print(hello_str.endswith("ld"))
# 查找指定字符串
# 返回索引
print(hello_str.find("llo"))
"""
index也可以查找指定字符串
find和index区别:
find在找不到指定字符串的情况下,会返回-1;
index会报错
"""
print(hello_str.find("abc"))
# 替换字符串
"""
replace方法执行完成后,会返回一个新的字符串
不会修改原有字符串的内容
"""
print(hello_str.replace("ld", "kk"))
print(hello_str)

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)