Home > Software design >  How Can I Split string every nth character, And Display like This?
How Can I Split string every nth character, And Display like This?

Time:02-12

I want to print the following string:

1234567890

as follows:

123
456
789
0

How can I do this in Python?

CodePudding user response:

You could use re.findall here:

inp = '1234567890'
output = '\n'.join(re.findall(r'.{1,3}', inp))
print(output)

This prints:

123
456
789
0

CodePudding user response:

try this:

x = "1234567890"
n = 3

y = list([x[i:i n] for i in range(0, len(x), n)])

for chunk in y:
    print(chunk)
  • Related