harsha
harsha

Reputation: 51

String pattern alteration

Suppose I have a function that could do the following

ex: '1100001' ->breaking into two parts 1::2 and 0::2-> and join it to get '1001001'. how to get the original string '1100001' from the output

Upvotes: 1

Views: 66

Answers (1)

akuiper
akuiper

Reputation: 214957

You can create a new list of the same length of the original one and assign corresponding elements to it:

s = '1001001'

s1 = list(s)
s1[1::2], s1[0::2] = s[:(len(s) // 2)], s[(len(s) // 2):]

''.join(s1)
# '1100001'

Upvotes: 1

Related Questions