Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将字符串转换为列表_Python_Python 3.x - Fatal编程技术网

如何在python中将字符串转换为列表

如何在python中将字符串转换为列表,python,python-3.x,Python,Python 3.x,我的字符串为: myString = 'example' 如何将其转换为以下列表: lst = ['example'] 以有效的方式?最自然的方式是正确的: mystr = 'example' lst = [mystr] 另外,不要将变量命名为str;它覆盖内置str。您可以使用append函数来实现: lst = [] str = 'example' lst.append(str) str='example' 现在,当s='example'时,l将包含['e','x','a','m

我的字符串为:

myString = 'example'
如何将其转换为以下列表:

lst = ['example'] 

以有效的方式?

最自然的方式是正确的:

mystr = 'example'
lst = [mystr]

另外,不要将变量命名为str;它覆盖内置str。

您可以使用append函数来实现:

lst = []
str = 'example'

lst.append(str)
str='example'

现在,当s='example'时,l将包含['e','x','a','m','p','l','e']

s]或s.split。虽然s.split要慢得多。可能是
l=list(str)