python 字符串切割 maxsplit

python 字符串切割 maxsplit

  1. my_str.split(str1, maxsplit)
  2. str1 可以不写,默认是空白字符(" " “\t” “\n”)
  3. 将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']
发布了56 篇原创文章 · 获赞 17 · 访问量 2178

猜你喜欢

转载自blog.csdn.net/LanlanDeming/article/details/103318399