0
点赞
收藏
分享

微信扫一扫

python字符串:字符串的查找和替换

沪钢木子 2022-02-27 阅读 127
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)

举报

相关推荐

0 条评论