Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a str to a single element list in python

Tags:

python

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?

like image 667
bigbug Avatar asked Aug 22 '12 03:08

bigbug


People also ask

Can you convert a string to a list in Python?

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.


2 Answers

Just put square brackets

>>> var_1 = "hello" >>> [var_1] ['hello'] 
like image 129
John La Rooy Avatar answered Sep 29 '22 11:09

John La Rooy


Does var1 = [var1] accomplish what you're looking for?

like image 33
RocketDonkey Avatar answered Sep 29 '22 11:09

RocketDonkey