How can I split a string in Python?

Possible Duplicates: Split python string every nth character? What is the most “pythonic” way to iterate over a list in chunks?

I need to split a string into equal parts. For example, if I have this string:

string = "123456781234567812345678"

I need to cut every 8 characters:

["12345678", "12345678", "12345678"]

Answers


This is actually a great application for a python list comprehension one-liner!

my_str="123456781234567812345678"
splits=[my_str[x:x+8] for x in range(0,len(my_str),8)]
//splits = ["12345678","12345678","12345678"]

Let me know if you have any questions.


Need Your Help

Is it possible to create Compound-Primary-key in a model in lift's mapper

scala lift mapper

Is it possible to create compound-Primary-key in Models in lift ? If yes, how ?

Exception while reading contact list

android android-contacts

Hi guys i'm getting the following Exception while readying contact list.