How to subtract two strings?

后端 未结 8 2037
执笔经年
执笔经年 2020-12-30 00:53

I have a long string, which is basically a list like str=\"lamp, bag, mirror,\" (and other items)

I was wondering if I can add or subtract some items, i

8条回答
  •  醉梦人生
    2020-12-30 01:02

    If you have two strings like below:

    t1 = 'how are you'
    t2 = 'How is he'
    

    and you want to subtract these two strings then you can use the below code:

    l1 = t1.lower().split()
    l2 = t2.lower().split()
    s1 = ""
    s2 = ""
    for i in l1:
      if i not in l2:
        s1 = s1 + " " + i 
    for j in l2:
      if j not in l1:
        s2 = s2 + " " + j 
    
    new = s1 + " " + s2
    print new
    

    Output will be like:

    are you is he

提交回复
热议问题