字符串与列表之间的转换

字符串转列表函数 -- split

c = 'a|b|c|d'
print(c.split('|'))  #['a', 'b', 'c', 'd']
print(c.split('|',1)) # ['a', 'b|c|d']

f = 'a~b~c'
print(f.split('~')) #['a', 'b', 'c']

列表/元组转字符串的函数 -- join

test = ['a','b','c']
new_str = '.'.join(test)
print(new_str)
test_tuple = ('abc','d','e')
new_tuple_str = '.'.join(test_tuple)
print(new_tuple_str)
sort_str = 'adfvcbeghij'
res = sorted(sort_str)
print("res=",res)
print(''.join(res))
print('|'.join(res))

posted @ 2022-08-14 21:10  repinkply  阅读(29)  评论(0)    收藏  举报