1

That's the source code:

def revers_e(str_one,str_two):
    for i in range(len(str_one)):
        for j in range(len(str_two)):
            if str_one[i] == str_two[j]:
               str_one = (str_one - str_one[i]).split()
               print(str_one) 
            else:
               print('There is no relation')  

if __name__ == '__main__':
str_one = input('Put your First String: ').split()
str_two = input('Put your Second String: ')
print(revers_e(str_one, str_two))

How can I remove a letter that occurs in both strings from the first string then print it?

Knox Root
  • 141
  • 1
  • 2
  • 12

4 Answers4

0

First of all you don't need to use a pretty suboptimal way using range and len to iterate over a string since strings are iterable you can just iterate over them with a simple loop.

And for finding intersection within 2 string you can use set.intersection which returns all the common characters in both string and then use str.translate to remove your common characters

intersect=set(str_one).intersection(str_two)

trans_table = dict.fromkeys(map(ord, intersect), None)
str_one.translate(trans_table)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0
def revers_e(str_one,str_two):
    for i in range(len(str_one)):
        for j in range(len(str_two)):
          try:

            if str_one[i] == str_two[j]:
               first_part=str_one[0:i]
               second_part=str_one[i+1:]
               str_one =first_part+second_part
               print(str_one)

            else:
               print('There is no relation')

          except IndexError:
                return


str_one = input('Put your First String: ')
str_two = input('Put your Second String: ')
revers_e(str_one, str_two)

I've modified your code, taking out a few bits and adding a few more.

str_one = input('Put your First String: ').split()

I removed the .split(), because all this would do is create a list of length 1, so in your loop, you'd be comparing the entire string of the first string to one letter of the second string.

  str_one = (str_one - str_one[i]).split()

You can't remove a character from a string like this in Python, so I split the string into parts (you could also convert them into lists like I did in my other code which I deleted) whereby all the characters up to the last character before the matching character are included, followed by all the characters after the matching character, which are then appended into one string.

I used exception statements, because the first loop will use the original length, but this is subject to change, so could result in errors.

Lastly, I just called the function instead of printing it too, because all that does is return a None type.

83457
  • 203
  • 1
  • 11
0

How about a simple pythonic way of doing it

def revers_e(s1, s2):
    print(*[i for i in s1 if i in s2])    # Print all characters to be deleted from s1
    s1 = ''.join([i for i in s1 if i not in s2])    # Delete them from s1

This answer says, "Python strings are immutable (i.e. they can't be modified). There are a lot of reasons for this. Use lists until you have no choice, only then turn them into strings."

Community
  • 1
  • 1
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0

These work in Python 2.7+ and Python 3

Given:

>>> s1='abcdefg'
>>> s2='efghijk'

You can use a set:

>>> set(s1).intersection(s2)
{'f', 'e', 'g'}

Then use that set in maketrans to make a translation table to None to delete those characters:

>>> s1.translate(str.maketrans({e:None for e in set(s1).intersection(s2)}))
'abcd'

Or use list comprehension:

>>> ''.join([e for e in s1 if e in s2])
'efg'

And a regex to produce a new string without the common characters:

>>> re.sub(''.join([e for e in s1 if e in s2]), '', s1)
'abcd'
dawg
  • 98,345
  • 23
  • 131
  • 206