list()
函数。 str_list = list("hello")
,结果为 ['h', 'e', 'l', 'l', 'o']
。
在Python中,我们可以使用内置的split()函数将字符串转换为数组,split()函数通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串。
步骤如下:
1、定义一个字符串。
2、使用split()函数将字符串转换为数组。
代码如下:
定义一个字符串 str = "Hello, World!" 使用split()函数将字符串转换为数组 arr = str.split(", ") 打印数组 print(arr)
在这个例子中,我们使用", "作为分隔符,所以split()函数会将字符串"Hello, World!"分割为两个元素,即["Hello", "World!"]。