python - python:python 中的 string.replace() 是如何工作的?
问题描述
在下面的示例中,为什么不替换第一个“让”而不是替换第二个和第三个。谁能帮我为什么选择第二和第三而不是第一和第二'让'?
song = 'cold, cold heart'
print (song.replace('cold', 'hurt'))
song = 'Let it be, let it be, let it be, let it be'
'''only two occurences of 'let' is replaced'''
print(song.replace('let', "don't let", 2))
o/p
hurt, hurt heart
Let it be, don't let it be, don't let it be, let it be
解决方案
string.replace()
是区分大小写的。"Let"
与 不同"let"
,因此当方法查找 的出现时"let"
,它会被忽略。
推荐阅读
- javascript - 查询选择器查找动态元素
- reactjs - firebase.firestore() 不是函数,我尝试了所有解决方案但没有解决
- flutter - 集团图书馆状态
- javascript - 移除对 React 代码中顺序 id 值的依赖
- python - 如何使用 BeautifulSoup 抓取评论的“查看”计数?
- node.js - Nuxt 每隔几分钟就崩溃一次
- flutter - 无法在 Visual Studio Code 中以颤振颜色删除断点
- android - android webview 中的 Django csrf 验证失败。android webview 无法验证 django csrf 令牌
- python - Python - 区分相似的图像
- bison - 野牛移位/减少与平衡括号语法的冲突