divide a string of n into n chars, split () of n n

5

I have the following text: "ola sou o Allan"

I would like to have this text divided by eg 2 in 2 chars. How can I do this?

    
asked by anonymous 20.07.2016 / 16:33

1 answer

5

So:

texto = "ola sou o Allan"
lista = [texto[i:i+2] for i in range(0, len(texto), 2)]

I pulled it out .

Running:

>>> texto = "ola sou o Allan"
>>> lista = [texto[i:i+2] for i in range(0, len(texto), 2)]
>>> lista
['ol', 'a ', 'so', 'u ', 'o ', 'Al', 'la', 'n']

According to the English response, 2 may be variable:

>>> n = 2
>>> texto = "ola sou o Allan"
>>> lista = [texto[i:i+n] for i in range(0, len(texto), n)]
>>> lista
['ol', 'a ', 'so', 'u ', 'o ', 'Al', 'la', 'n']
    
20.07.2016 / 16:38