字符串与列表间转换
字符串分割成列表
sentence = "Hello world!"
words = sentence.split()
print(type(words))
print(words)
代码输出:
<class 'list'>
['Hello', 'world!']
字符串列表拼接成字符串
words = ["hello", "python!"]
sentence = "-".join(words)
print(type(sentence))
print(sentence)
代码输出:
<class 'str'>
hello-python!