python如何把字符串放到列表


要把字符串放到列表中,可以使用以下方法:

    使用split()方法:将字符串根据指定的分隔符分割成多个子字符串,并返回一个包含这些子字符串的列表。
string = "hello world"list = string.split()# 默认以空格作为分隔符print(list)# ['hello', 'world']
    直接使用列表推导式:
string = "hello world"list = [char for char in string]print(list)# ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
    使用list()方法:
string = "hello world"list = list(string)print(list)# ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']


上一篇:C#中Session失效时间怎么设置

下一篇:elasticsearch如何根据条件删除数据


python
Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索