Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing first and last punctuation from python

I have the following problem, I want to remove the punctuation marks at the beginning and at the end but leave the ones in between.
Example. \

[':Hamburger{', 'word>', 'don´t', 'isn´t,'] 

and this should turn

['Hamburger', 'word', 'don´t', 'isn´t']

So if anyone can help me, I would appreciate it

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use a regular expression for this. Just import re at the start of the file, then you could use the following line to replace all punctuation and the start or end of the line:

for el in list:
    el = re.sub("^[^\w\s]|[^\w\s]$", "", el)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading