温馨提示×

python如何把字符串放到列表

小亿
426
2024-03-19 14:37:30
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:python怎么把字符串转列表

0