0

The split method for strings only takes a parameter for the delimiter, but how do I easily split by every (or every other, etc) letter? This method works to split by every letter, but seems clumsy for a simple task.

a=' '.join(string.ascii_lowercase).split()

I suppose a function could do this:

def split_index(string,index=1):
    split=[]
    while string:
        try:
            sect = string[:index]
            string = string[index:]
            split.append(sect)
        except IndexError:
            split.append(string)
    return split

print(split_index('testing'))   # ['t', 'e', 's', 't', 'i', 'n', 'g']
print(split_index('testing',2)) # ['te', 'st', 'in', 'g']

I am surprised if no one has wished for this before, or if there is not a simpler built in method. But I have been wrong before. If such a thing is not worth much, or I have missed a detail, the question can be deleted/removed.

derz00
  • 11
  • 3
  • @abccd The OP doesn't show a knowledge that strings are iterable, which is required to understand that answer is applicable to this question. – Gareth Latty Mar 19 '18 at 23:51
  • @abccd You're likely right, in a way. I did not see that question in my searching, because the wording is different. Also, I do not think that I would have realized, by looking at it, that it asks the same thing. – derz00 Mar 20 '18 at 23:16
  • I introduced the topic of splitting into sections of more than one character, since I thought that would help me find an answer. – derz00 Mar 20 '18 at 23:25

2 Answers2

0

Strings are iterables of characters, so splitting it into characters is as simple as iterating over it. One way of doing that is just giving it to the list() built-in:

list("testing") # ['t', 'e', 's', 't', 'i', 'n', 'g']

The docs also provide a recipe for a grouper() method that will take an iterable and group it into chunks of the given size, as strings are iterables, this just works on them. This can be very efficient as it is lazy and utilises the fast itertools functions:

import itertools

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=fillvalue)

E.g:

groups = list(grouper("testing", 2)) # [('t', 'e'), ('s', 't'), ('i', 'n'), ('g', None)]]
["".join(char for char in group if char) for group in groups] # ['te', 'st', 'in', 'g']

Obviously, if you are just iterating over the characters, you don't need to make a list - just iterate straight over the string or the grouper() iterable.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • The `list()` function is what I was looking for. I think I have seen it used that way before, but hadn't registered it well enough. But thank you for the other information as well. – derz00 Mar 20 '18 at 23:10
  • I realize that I did not think of it that a string is an iterable. This question will be flagged as duplicate of [this](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – derz00 Mar 20 '18 at 23:38
0

You can try doing it this way:

string = "testing"

def splitByN(string,n):
    return [string[i:i+n] for i in range(0,len(string),n)]

print(splitByN(string,2))

Output:

['te', 'st', 'in', 'g']
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29