首页 > 解决方案 > 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)

标签: python

解决方案


strA = "My dog love hot dog, milk, and candy."
strB = "dog and bread"

strA = strA.replace('dog,',strB);
print(strA);

这不是一个动态的解决方案。但可以解决这个问题


推荐阅读