- There is no attribute
Split
for strings in any version of python.
- If you intended to write
split
, the aforementioned method requires a character in all versions of python
python-2.x
>>> string = "abcdefghijklmnopqrstuvwx"
>>> string = string.split(0 - 3)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: expected a character buffer object
python-3.x
>>> string = "abcdefghijklmnopqrstuvwx"
>>> string = string.split(0 - 3)
Traceback (most recent call last):
File "python", line 2, in <module>
TypeError: Can't convert 'int' object to str implicitly
That said...
You can use the following code to split into equal groups:
def split_even(item, split_num):
return [item[i:i+split_num] for i in range(0, len(item), split_num)]
As such:
>>> split_even("abcdefghijklmnopqrstuvwxyz", 4)
['abcd', 'efgh', 'ijkl', 'mnop', 'qrst', 'uvwx', 'yz']
>>> split_even("abcdefghijklmnopqrstuvwxyz", 6)
['abcdef', 'ghijkl', 'mnopqr', 'stuvwx', 'yz']
>>> split_even("abcdefghijklmnopqrstuvwxyz", 13)
['abcdefghijklm', 'nopqrstuvwxyz']
>>>