How to remove characters from a string?

Asked

Viewed 37,862 times

6

The program must read two strings and remove from the first string all the letters that occur in the second string. Example: Be the strings "chocolate" and "hollow", then the program should print "hlte". How to solve the problem using string knowledge? Pseudocode:

 string1 = raw_input()
 string2 = raw_input()
 stringresultante = caracteres da string1 - caracteres da string2
 print stringresultante
Show 1 more comment

6 answers

11


How will you remove a specific character from a String using another String, you will need to make a loop to travel the String which has the characters "hollow" to check in its second String "chocolate".

See the example below.

>>>
>>> a = "a!b@c#d$"
>>> b = "!@#$"
>>> for i in range(0,len(b)):
...  a =a.replace(b[i],"")
...
>>> print a
abcd
>>>
  • Thank you very much. Your answer was simple and reconciled the subjects I have studied.

2

I wore something similar to Gustavo Sampaio and Marco Souza

ex1 = "chocolate"
ex2 = "oca"

for letra in ex2:
   if letra in ex1:    
      ex1 = ex1.replace(letra,'')


print(ex1) 

The output will be:

hlte

What this code does is loop for each letter of the second string, then check if that letter is in the first example and if it is true it will remove that letter from the first string and consider that Ex1 becomes the word "chocolate", but without the letters of ex2

2

word1 = 'chocolate'
word2 = 'oca'

for i in word2:
    word1 = word1.replace(i, '')
    
print(word1)
  • Welcome to Stack Overflow in English. This code may be a solution to the question, but your answer might be better if you [Edit] and include an explanation of the key points of the code. The goal is not only to help those who asked the question, but the next visitors as well. Read more on Code-only answers - What to do?.

1

An interesting way to resolve this issue is by using the function filter along with lambda. For this we can assemble the code as follows:

string1 = 'chocolate'
string2 = 'oca'

resultado = ''.join(filter(lambda i: i not in string2, string1))

print(resultado)

Running this code we will receive as a result:

hlte

In case you want to specify the value of string1 and of string2, we can use the following code:

string1 = input('Digite uma string: ')
string2 = input('Caracteres a ser removidos: ')

resultado = ''.join(filter(lambda i: i not in string2, string1))

print(resultado)

1

Well, I think the most pythonic solution would be to use the method str.translate:

string1 = 'chocolate'
string2 = 'oca'

char_rep={k: '' for k in string2}

string1=string1.translate(str.maketrans(char_rep))

print(string1)

Returns:

hlte

1

Another alternative would be this:

string1 = "chocolate"
string2 = "oca"

result = ''.join(char for char in string1 if char not in string2)

# 'hlte'
print(result)

I mean, we created a Generator Expression (generating expression, in free translation) - which is also used in list comprehensions - which says that:

For each character in string1, return the one who is not in string2.

(char for char in string1 if char not in string2)

And we use the method str.join() to join the characters chosen, that is, to join the characters that met that condition, into a single string, separating them by '', which simply serves to keep the characters very close to each other, as in a normal text.

''.join(...)

That would be the same as:

string1 = 'chocolate'
string2 = 'oca'

result = ''

for char in string1:
    if char not in string2:
        result += char

And this solution (given at the beginning) works on both Python 2 and Python 3.

The point here is that you will not, in fact, be removing characters from string basis (that is, you will not be modifying to string basis), but will be creating a new, from of string groundwork.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.