Home > Software design >  Is there a way of splitting a word into 3 different parts in python?
Is there a way of splitting a word into 3 different parts in python?

Time:11-27

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

CodePudding user response:

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
  • Related