Hi,
I have two strings
str1 = 'ABCdef'
str2 = 'ABCdaf'
now I want to know which position (character) is different.
Desired output is 4
in str1: e
same position is str2: a
If you want to know where the 'e' is, you use index: st1.index('e')
. If you want to know what is at that location you use slicing: st1[4]
.
I don't know before hand,
1. check is two strings equal or not
2. If not equal. then get which position is not equal
(Sep-16-2019, 12:40 PM)SriRajesh Wrote: [ -> ]I don't know before hand,
1. check is two strings equal or not
2. If not equal. then get which position is not equal
This is not reasonable approach. Why should you process strings twice? When are strings not equal? Strings are not equal if their corresponding characters don't match. So you can in one iteration compare corresponding characters and get indices of non-equal ones.
str1= 'ABCdef'
str2= 'ABCdaf'
u=zip(str1,str2)
print (u)
for i,j in u:
if i==j:
print (i,'--',j)
else:
print (i,' ',j)
I want to get the positions where inequality
(Sep-16-2019, 01:17 PM)SriRajesh Wrote: [ -> ]I want to get the positions where inequality
Maybe something along these lines:
>>> for i, pair in enumerate(zip('spam', 'eggs')):
... print(i, pair)
...
0 ('s', 'e')
1 ('p', 'g')
2 ('a', 'g')
3 ('m', 's')