【发布时间】:2021-02-23 05:52:08
【问题描述】:
所以我需要连接两个字符串中的相似字符。例如,String1 = 'harry' 和 String2 = 'hermione'。输出应该是hrrhr。但是,如果没有常用字符,我必须打印 Nothing in common。 例如,string1 = 'dean' 和 string2 = 'tom'。在这种情况下,输出必须是Nothing in common。
我想我已经弄清楚了连接部分。但是当字符串不共享公共字符时,我会坚持打印“Nothing in common”。这是我完成一半的代码:
str1 = input('enter string1:')
str2 = input('enter string2:')
empty_str1 = ''
empty_str2 = ''
for i in str1:
if i in str2:
empty_str1 += i
for j in str2:
if j in str1:
empty_str2 += j
print(empty_str1 + empty_str2)
当我输入“harry”和“hermione”时,我得到 hrrhr 作为输出。但是当我输入像'dean'和'tom'这样的字符串时,我真的不知道如何打印'Nothing in common'。请帮忙
【问题讨论】:
标签: python python-3.x string concatenation