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

How to remove punctuation marks from a list of strings without using libraries in Python 3.x?

I need to remove all punctuation marks (without being specific) and ‘\n’ from a list of strings as for example:

l = ['``What ails you, Sister Erin, that your face\n', 'Is, like your mountains, still bedewed with tears?\n', 'As though some ancient sorrow or disgrace,\n']

output:

['What ails you Sister Erin that your face', 'Is like your mountains still bedewed with tears', 'As though some ancient sorrow or disgrace']

I’m trying to do it without using libraries.

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 don’t have to use any external libraries for this. The basic str type can deal with this.

See below:

l = ['``What ails you, Sister Erin, that your face\n', 'Is, like your mountains, still bedewed with tears?\n',
     'As though some ancient sorrow or disgrace,\n']

l = ["".join([char for char in line if char.isalnum() or char == " "]) for line in l]
print(l)

You just need to ignore all characters which are not alphanumeric or space

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