python - Python中如何快速合并字符串
问题描述
我有这样的字符串:
strA = "Today is hot. My dog love hot dog, milk, and candy."
strB = "hot dog and bread"
我希望输出字符串像:
"Today is hot. My dog love hot dog and bread, milk, and candy."
不是
"Today is hot dog and bread. My dog love hot dog, milk, and candy."
我尝试使用str.find()
,但它不适合。
if strA.find(strB) != -1:
x = strA.find(strB)
listA = list(strA)
listA.insert(x, strB)
output = ''.join(listA)
解决方案
strA = "My dog love hot dog, milk, and candy."
strB = "dog and bread"
strA = strA.replace('dog,',strB);
print(strA);
这不是一个动态的解决方案。但可以解决这个问题
推荐阅读
- python - 正则表达式在恰好两个正斜杠之后匹配字符串
- android-notifications - Android 9 中未显示通知
- javascript - Stripe Node js 向客户收费
- swift - Django Rest Framework:iOS Swift 客户端处理文件上传时出错
- javascript - 我尝试在 Twitter 集成上通过 ng2-twitter 安装依赖项后出现错误
- sql - 根据日期选择查询
- php - 为特定页面指定 og:image
- sql - SQL Server:如何中途中断 SQL 查询?
- mysql - 在mysql查询中将数字视为零
- python - Python:从线程事件更新 tkinter 画布小部件