I want to convert a string to a list without separating the characters. I can do:
cadena = "hola"
If now I do:
list(cadena)
The interpreter's response is:
['h', 'o', 'l', 'a']
But suppose I want to have this other response:
['hola']
How can I do it without doing: lista = ["hola"]
? Is there something like a function that joins all the separated characters in a list to a list of a single string? Or a function that joins me all the elements, whether numbers or strings, of a list and presents them to me as a string of a list of a single element, that element being said string?
Just put the variable inside
[]
:As opposed to using
list()
which builds the list by iterating over the string, this is equivalent to:The method you raise in your autoresponder works, but using the constructor of
list()
and then the methodjoin()
is much more inefficient as you first iterate over the string to break it into characters and then iterate over the list of characters to pass it back to a string.This is the easiest and most direct way to do it, if you want to add multiple strings:
I'm going to use the join method:
Well, the answer from @FJSevilla is good, but for his answer to work, he has to already have all the strings ready that you are going to put in the list, but what happens if you want to put one by one and at different times in the script.
In that case, it would be best to use the append function.
Let's go to the code
I hope it helps you @Mr. Baldan