python 字符串切割 maxsplit
- my_str.split(str1, maxsplit)
- str1 可以不写,默认是空白字符(" " “\t” “\n”)
- 将my_str 这个字符串按照str1 进行切割, maxsplit 割几次
my_str = "hello world itcast and itcastcpp"
my_str1 = my_str.split(" ")
print(my_str1)
my_str2 = my_str.split(" ", 1)
print(my_str2)
my_str3 = my_str.split() # 用的最多
print(my_str3)
my_str4 = my_str.split("itcast")
print(my_str4)
# 输出结果是
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello', 'world itcast and itcastcpp']
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello world ', ' and ', 'cpp']