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 add space in alphabets and digits using regex python

I want to separate string from the digits using regex. I don’t want to replace anything just add space. I don’t want to add space in digit and special character or string and special character.
for example

 "A-21PHASE-1,ASHOK VIHARA-21, PHASE-1, - ASHOK VIHAR110052" 

output for above example should look like,

"A-21 PHASE-1,ASHOK VIHARA-21, PHASE-1, - ASHOK VIHAR 110052"

in this example I want to add space between alphabets and number. there are number attached with ‘-‘ or any special character , I don’t want to do anything with 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 :

User re.sub:

thestring = re.sub(r'(\d)([A-Z])', r'\1 \2', thestring)
thestring = re.sub(r'([A-Z])(\d)', r'\1 \2', thestring)

First one puts spaces between digits and capital letters, second one between capital letters and digits.

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