在python中,split()函數用于將字符串分割為子字符串,并返回一個由子字符串組成的列表。split()函數可以使用不同的分隔符來分割字符串。下面是split()函數的幾種常用用法:
sentence = "Hello World"
words = sentence.split()
print(words) # ['Hello', 'World']
sentence = "Hello,World"
words = sentence.split(',')
print(words) # ['Hello', 'World']
text = "Hello\nWorld"
lines = text.split('\n')
print(lines) # ['Hello', 'World']
sentence = "Hello-World,Python"
words = sentence.split('-')
print(words) # ['Hello', 'World,Python']
words = sentence.split('-')
print(words) # ['Hello', 'World', 'Python']
sentence = "Hello World Python"
words = sentence.split(maxsplit=1)
print(words) # ['Hello', 'World Python']
這些是split()函數在python中的一些常見用法,可以根據具體的需求來選擇合適的用法。