1

In my text, I have special characters such as em dashes and guillemets (pointed quotation marks) which are not removed by omitting string.punctuation

What is the correct way to remove this type of punctuation from strings in Python 3?

import string

mystring = ' »De fleste – digitale –'
mystring.translate(str.maketrans('', '', string.punctuation))
' »De fleste – digitale –'
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 1
    Does [`this`](https://stackoverflow.com/questions/33930767/trying-to-remove-character-em-dash-â€-in-python-using-regex) help? – Mayank Porwal Oct 31 '20 at 19:54

2 Answers2

1

Try this :

import itertools as it

mystring = ' »De fleste – digitale –'
newstring = ''
mystring = newstring.join(it.filterfalse(lambda x: x in '»–', mystring))
print(mystring) #=> " De fleste  digitale"
dspr
  • 2,383
  • 2
  • 15
  • 19
0

here's a simple solution:

    mystring = ' »De fleste – digitale –'
    print(''.join(x for x in mystring if x not in ['»', '–', '?', '!', '«']))

you can add whatever you want to omit to the list inside the .join() function