How to remove characters from string?

Question:

How to remove user defined letters from a user defined sentence in Python?

Hi, if anyone is willing to take the time to try and help me out with some python code.

I am currently doing a software engineering bootcamp which the current requirement is that I create a program where a user inputs a sentence and then a user will input the letters he/she wishes to remove from the sentence.

I have searched online and there are tons of articles and threads about removing letters from strings but I cannot find one article or thread about how to remove user defined letters from a user defined string.

import re
sentence = input("Please enter a sentence: ")
letters = input("Please enter the letters you wish to remove: ")
sentence1 = re.sub(letters, '', sentence)
print(sentence1)

The expected result should remove multiple letters from a user defined string, yet this will remove a letter if you only input 1 letter. If you input multiple letters it will just print the original sentence. Any help or guidance would be much appreciated.

Asked By: Deeface

||

Answers:

You can use a list comprehension:

result = ''.join([x for x in sentence if x not in letters])
Answered By: raratiru
>>> sentence1 = re.sub(str([letters]), '', sentence)

Preferably with letters entered in the form letters = 'abcd'. No spaces or punctuation marks if necessary.

.

Edit:

These are actually better:

>>> re.sub('['+letters+']', '', sentence)
>>> re.sub('['+str(letters)+']', '', sentence)

The first also removes ' if it appears in the string, although it is the prettier solution

Answered By: FailSafe

If I understood correctly we can use str.maketrans and str.translate methods here like

from itertools import repeat

sentence1 = sentence.translate(str.maketrans(dict(zip(letters, repeat(None)))))

What this does line by line:

  • create mapping of letters to None which will be interpreted as “remove this character”

    translation_mapping = dict(zip(letters, repeat(None))
    
  • create translation table from it

    translation_table = str.maketrans(translation_mapping)
    
  • use translation table for given str

    sentence1 = sentence.translate(translation_table)
    

Test

>>> sentence = 'Some Text'
>>> letters = 'te'
>>> sentence.translate(str.maketrans(dict(zip(letters, repeat(None)))))
'Som Tx'

Comparison

from timeit import timeit
print('this solution:',
      timeit('sentence.translate(str.maketrans(dict(zip(letters, repeat(None)))))',
             'from itertools import repeatn'
             'sentence = "Hello World" * 100n'
             'letters = "el"'))
print('@FailSafe solution using `re` module:',
      timeit('re.sub(str([letters]), "", sentence)',
             'import ren'
             'sentence = "Hello World" * 100n'
             'letters = "el"'))
print('@raratiru solution using `str.join` method:',
      timeit('"".join([x for x in sentence if x not in letters])',
             'sentence = "Hello World" * 100n'
             'letters = "el"'))

gives on my PC

this solution: 3.620041800000024
@FailSafe solution using `re` module: 66.5485033
@raratiru solution using `str.join` method: 70.18480099999988

so we probably should think twice before using regular expressions everywhere and str.join‘ing one-character strings.

Answered By: Azat Ibrakov

Your code doesn’t work as expected because the regex you provide only matches the exact combination of letters you give it. What you want is to match either one of the letters, which can be achieved by putting them in brackets, for example:

import re
sentence = input("Please enter a sentence: ")
letters = input("Please enter the letters you wish to remove: ")
regex_str = '[' + letters + ']'
sentence1 = re.sub(regex_str, '', sentence)
print(sentence1)

For more regex help I would suggest visiting https://regex101.com/

Answered By: TheArmoza
user_word = input("What is your prefered sentence?  ")  

user_letter_to_remove = input("which letters would you like to delete? ")

#list of letter to remove

letters =str(user_letter_to_remove)

for i in letters:
    user_word = user_word.replace(i,"")

print(user_word)
Answered By: eli_257
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.