+ 5

Is there a way to split a string into a list without a seperator?

I want to convert a string of unknown length into a list where every object has 6 chars (except the last one, if not len(string)%6==0) How do I do it?

3rd Apr 2017, 9:27 PM
Supersebi3
Supersebi3 - avatar
4 Respostas
4th Apr 2017, 1:25 AM
Luca Garrera
+ 5
@Luca Garrera nice!
4th Apr 2017, 5:35 AM
Supersebi3
Supersebi3 - avatar
0
def my_splitter(astr,size): return [astr[i*size:(i*size)+size] for i,x in enumerate(astr)if i*size<len(astr)] print(my_splitter("here is a string example too",6)) print(my_splitter("here is a string example too",3))
4th Apr 2017, 5:38 AM
richard
- 2
There is probably an easy way to do it, but for fun I wrote a function that will remove anything in the list that is more than 6 chars in length. myList="me you television hello balloon people peoples".split() def fixIt(x): tempList=[] for i in myList: if len(i)<=6: tempList.append(i) return tempList myList = fixIt(myList)
4th Apr 2017, 12:32 AM
LordHill
LordHill - avatar