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 trim the characters in the string?

I have a list.

file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']

I want to trim the strings in the list to remove the characters including . sign and the numbers.

Expected output:

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

file_name = ['a.pdf','b.pdf','c.pdf','d.pdf']

>Solution :

You can use regex with pattern as r'\.\d+'. This will literally match . followed by one ore more digits.

>>> import re
>>> file_name = ['a.3903902.pdf','b.3432312.pdf','c.239002191.pdf','d.23423192010.pdf']
>>> [re.sub(r'\.\d+', '', f) for f in file_name]
['a.pdf', 'b.pdf', 'c.pdf', 'd.pdf']
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