Slicing String and saving the slices in a PYTHON List

2

Hello, I'd like to understand a little more about strings in PYTHON . I'm developing a program, which consists of:

Get a STRING

Separate STRING every 3 characters

And save each part in a LIST

EX:

nome = "guilherme"

lista = ["gui","lhe","rme"]

    
asked by anonymous 11.11.2018 / 06:26

1 answer

3

You can use slicing to access slices from an array (or string). nome[0:3] returns "gui" nome[3:6] returns "you" And so on.

nome = "guilherme"
lista = [nome[i:i+3] for i in range(0, len(nome), 3)]
    
11.11.2018 / 06:59