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

Multiple slicing string in Python

My goal is to slice string several time. That is, when [2,3,1] given, string ‘ABCDEF’ should be ‘AB’,’CDE’ and ‘F’.Is there a function prepared in python for this task or How to make function with built-in functions?

    list [2,3,1] given.
    string 'ABCDEF' given,
    func('ABCDEF',[2,3,1])  --> 'AB','CDE' and 'F' 

>Solution :

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

If you are open to regex, one option might be:

inp = "ABCDEF"
parts = re.findall(r'(.{2})(.{3})(.)', inp)[0]
print(parts)  # ('AB', 'CDE', 'F')
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