Slicing String and Saving the Slices in a PYTHON List

Asked

Viewed 238 times

3

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

Pick up a STRING

Separate STRING from 3 to 3 characters

And store each part in a LIST

EX:

nome = "guilherme"

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

1 answer

4


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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.