通过指定分隔符对字符串进行分隔
注意要拆分的字符串需要要一定规律的分隔符 ,比如"Python is a good language" 这个字符串中间都有个空格,那么我们可以通过这个空格来分隔这个字符串
string = "Python is a good language"
content = string.split(" ") # 通过空格来分隔string 字符串
print(content)
打印结果如下
string = "Python is a good language"
content = string.split(" ", 2) # 分隔前2个字符串
print(content)
打印结果如下
string = "Python is a good language"
content = string.split(" ")[3] # 字符串的下标是从0开始这里是获取分隔的第四个字符
print(content)
打印结果如下
string = "Hi:Tom"
content = string.split(":")
print(content)
key = string.split(":")[0]
print(key)
value = string.split(":")[1]
print(value)
string = "abc#def#xyz"
content = string.split("#")
print(content)
content1 = string.split("#")[0]
print(content1)
content2 = string.split("#")[1]
print(content2)
content3 = string.split("#")[2]
print(content3)
打印结果如下