在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中的一些常见用法,可以根据具体的需求来选择合适的用法。