I want to delete all the letters in two strings that are not the same between them.
For example:
str1 = 'abcdefg'
str2 = 'efghijk'
After removing the different characters I want to get:
str1 = 'efg'
str2 = 'efg'
I have the following code:
for i in str1:
if i not in str2:
str1 = str1.replace("i", "") #delete that element
#strings are immutable so I create a new string and remove that element
Then I would do the same with the other string.
However, when I print it out it does not delete all the elements I want it to. Or sometimes it doesn't delete any of the elements.
I have also tried it with a double for loop but it does not work correctly.
Is there something wrong with my logic?