-1

Running this code

a =', my city is changing . The country is changing . '
b =', my city is changing . The country is changing . '
print(a == b)

gets me a False.

An editor highlights the difference. Click here to reproduce.

enter image description here

How do I get the difference programmatically with Python?

martineau
  • 119,623
  • 25
  • 170
  • 301
JJJohn
  • 915
  • 8
  • 26
  • 1
    `How do I get the difference programmatically with Python?` What exactly do you want? Positions where the strings differ? Then check https://stackoverflow.com/questions/8545492/find-the-position-of-difference-between-two-strings –  Jul 19 '20 at 23:35

1 Answers1

2

You can use the following:

a =', my city is changing . The country is changing . '
b =', my city is changing . The country is changing . '
print([(i, t) for i, t in enumerate(zip(a, b)) if t[0] != t[1]])

which will print

[(21, ('\xa0', ' ')), (23, ('\xa0', ' '))]

This creates a list of 2-tuples for each character from each string and prints the indices of non-matching ones.

Selcuk
  • 57,004
  • 12
  • 102
  • 110