1

I have two texts and want to print the text that was added enter image description here i have two variables x and y which are a string. I would like it to look like this:

x = 'abcdefg'
y = 'abcdTHISefg'

print(TextCompare(x, y))

output:
> this
  • Does this answer your question? [Comparing two strings and returning the difference. Python 3](https://stackoverflow.com/questions/30683463/comparing-two-strings-and-returning-the-difference-python-3) – Bushmaster Oct 30 '22 at 09:39
  • the code that was sent there also shows me the text that follows the text I want to print, so no. – PolsatGraniePL Oct 30 '22 at 09:42

1 Answers1

0

If you are interested in finding what has been added between two strings then it seems like Python's difflib might be of help.

For example, the following gives this as an output on your test data:

import difflib

x = 'abcdefg'
y = 'abcdTHISefg'

diff = difflib.SequenceMatcher(None, x, y)

for tag, x_start, x_end, y_start, y_end in diff.get_opcodes():
    if tag == 'insert':
        print(y[y_start:y_end].casefold())

ukBaz
  • 6,985
  • 2
  • 8
  • 31