1

This is my string:

data = abdskjfdskjfjkdseiruiskdfdsdfjsdjkfdsdfsdjk 

I want to split my data into 10s

So, I want my output to look like:

abdskjfdsk
jfjkdseiru

and so on for the rest of the string.

I tried:

chunks, chunk_size = len(data), len(data)//10
[ data[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]

but it didn't change anything and my output was

data = abdskjfdskjfjkdseiruiskdfdsdfjsdjkfdsdfsdjk 

EDIT: This solved the issue:

import re
re.findall('.{10}', data)

Thank you !!

Mina
  • 11
  • 3

2 Answers2

1

Use re.findall

>>> import re
>>> re.findall('.{10}', data)
['abdskjfdsk', 'jfjkdseiru', 'iskdfdsdfj', 'sdjkfdsdfs']

. matches any character (except for line terminators)

{10} Quantifier — Matches exactly 10 times

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
1

when you have a string, you can subset by each of the elements of the string using brackets []. This is, if you want the first three elements fo your list you can use:

data[0:3]

next, we'll harness a list comprehension technique to do this process multiple times:

[data[character:character+10] for character in range(0, len(data),10)]