Soon
Soon

Reputation: 501

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' 

Upvotes: 0

Views: 156

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

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')

Upvotes: 2

Related Questions