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

Is there a way of splitting a word into 3 different parts in python?

char = "RV49CJ0AUTS172Y"

To get this output:

separated = "RV49C-J0AUT-S172Y"

I have tried:

split_strings = []
             n = 5
             for index in range(0, len(name), n):
                split_strings.append(name[index : index + n])

But that did not go as I wanted
This shows a good method but I want them on the same line with dashes between them

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 can do it in one-line, using re

import re
string = "RV49CJ0AUTS172Y"

separated = "-".join(re.findall('.{%d}' % 5, string))

print(separated)

[Output]

RV49C-J0AUT-S172Y
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