Ad

Extract Difference Between Two Strings

- 1 answer

Let's say I have two strings:

s1 = "hello how are you, this is a test"

s2 = "this is a test, testing testing."

Then, how can I extract the string of s2 which is not in s1 ?

diff = function(s1,s2) 
print(diff)

", testing testing."

Ad

Answer

Maybe this will help:

s1 = "hello how are you ?"

s2 = "hello"
if s2 in s1:
    print(s1.replace(s2, ''))
elif s1 in s2:
    print(s2.replace(s1, ''))
else:
    print('Not a substring')

Update

Use this then:

s1 = "hello how are you, this is a test"

s2 = "this is a test, testing testing."

diff = ''
for word in s2.split():
    if word not in s1.split():
        diff += word + ' '
print(diff)
Ad
source: stackoverflow.com
Ad