Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

This might help:

str.split(str="", num=string.count(str)).

str: any delimeter, by default it is space.

num: number of lines to be made.

You can get more info here.

Post a Comment for "How Can I Split A String In Python?"