I have a str
variable: var_1 = "hello"
, and I want to convert it to a single element list
, I try this:
>>> list(var_1) ['h', 'e', 'l', 'l', 'o']
which is not the ['hello']
that I want.
How do I do that?
Python String is a sequence of characters. We can convert it to the list of characters using list() built-in function. When converting a string to list of characters, whitespaces are also treated as characters. Also, if there are leading and trailing whitespaces, they are part of the list elements too.
Just put square brackets
>>> var_1 = "hello" >>> [var_1] ['hello']
Does var1 = [var1]
accomplish what you're looking for?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With