一、列表list转字符串str

命令(python2.x):''.join(list)

命令(python2.x):''.join(str(s) for s in list)

其中,引号中是字符之间的分割符,如“,”,“;”,“\t”,“*”等等

我的电脑是python3.6的,如:

>>> list = [1, 2, 3, 4, 5]
>>> print(''.join(str(s) for s in list1))
123456
>>> print('*'.join(str(s) for s in list1))
1*2*3*4*5*6

二、字符串str转列表list

命令:list(str)

其中,list表示列表,str将为str类型变量的名称

>>> str1 = '1234567'
>>> print(list(str1))
['1', '2', '3', '4', '5', '6', '7']
>>> print(list(map(int,str1)))
[1, 2, 3, 4, 5, 6, 7]
>>> str2 = '123 abc def $%#'
>>> print(str2.split()) # 或 print(str2.split(‘ ’))默认以空格分割
['123', 'abc', 'def', '$%#']
>>> str3 = "www.baidu.com"
>>> print(str3.split('.'))
['www', 'baidu', 'com']

相关文章:

  • 2022-01-21
  • 2021-07-09
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
猜你喜欢
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
相关资源
相似解决方案