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

Using regex to strip characters in a list of strings but keep any spaces

I am trying to split a list to separate each character in a string.

This is what I did:

import re

my_list = ['<!DOCTYPE html>']

my_list = [re.split('\s', char) for char in my_list[0]]

print(my_list)

#Returns [['<'], ['!'], ['D'], ['O'], ['C'], ['T'], ['Y'], ['P'], ['E'], ['', ''], ['h'], ['t'], ['m'], ['l'], ['>']]

Ideally, I would want it to return a list of each individual characters (not in their own list) and only a single space. How do I fix this?

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 :

Just use list on the string:

>>> list('<!DOCTYPE html>')
['<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 'h', 't', 'm', 'l', '>']
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