Python String split() 方法

Python String split() 方法

Python String split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。

Python String split() 语法

split() 方法语法:

str.split(str="", num=string.count(str))

Python String split() 参数

  • str - 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num - 分割次数。默认为 -1, 即分隔所有。

Python String split() 返回值

返回分割后的字符串列表。

Python String split() 示例1

以下实例展示了 split() 函数的使用方法:

#!/usr/bin/python3

str1 = "this is string example from apidemos.com....wow!!!"
print (str1.split( ))       # 以空格为分隔符
print (str1.split('i',1))   # 以 i 为分隔符
print (str1.split('w'))     # 以 w 为分隔符

输出:

Python String split() 方法

Python String split() 示例2

下面的示例演示split()函数在指定maxsplit时如何工作的例子。
maxsplit参数用于控制在解析字符串后要返回多少个分割。即使可能有多个分割,它也只做maxsplit参数定义的最大分割数。

word = 'api, for, demos, good examples.'

# maxsplit: 0
print(word.split(', ', 0))

# maxsplit: 4
print(word.split(', ', 4))

# maxsplit: 1
print(word.split(', ', 1))

输出:

Python String split() 方法

Python String split() 示例3

URL 简单分割。

我们在学习 python 爬虫的时候例如需要保存图片,图片名称的获取,可以依照下列方法:

#!/usr/bin/python3

url = "https://www.apidemos.com/python/image/123456.jpg"
#以 . 进行分隔
path =url.split(".")
print(path)

输出:

Python String split() 方法

/ 进行分隔:

#!/usr/bin/python3

url = "https://www.apidemos.com/python/image/123456.jpg"
#以 / 进行分隔
path =url.split("/")
print(path)

输出:

Python String split() 方法

我们在学习 python 爬虫的时候例如需要保存图片,图片名称的获取,可以依照下列方法:

#!/usr/bin/python3

url = "https://www.apidemos.com/python/image/123456.jpg"
#以 / 进行分隔
path =url.split("/")[-1]
print(path)

输出:

Python String split() 方法

赞(0)
未经允许不得转载:极客笔记 » Python String split() 方法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
Python OS模块
Python os.chown方法Python os.write() 方法Python os.pardir 方法
Python String模块
Python String capitalize()方法Python String count()方法Python String center()方法Python String expandtabs()方法Python String index()方法Python String isalnum()方法Python String endswith()方法Python String encode()方法Python String find() 方法Python String decode()方法Python String isalpha() 方法Python String isdigit() 方法Python String islower() 方法Python String isnumeric() 方法Python String isspace() 方法Python String istitle() 方法Python String isupper() 方法Python String join() 方法Python String len() 方法Python String ljust() 方法Python String lower() 方法Python String lstrip() 方法Python String maketrans() 方法Python String max() 方法Python String min() 方法Python String replace() 方法Python String rfind() 方法Python String rindex() 方法Python String rjust() 方法Python String rstrip() 方法Python String isdecimal() 方法Python String splitlines() 方法Python String split() 方法Python String startswith() 方法Python String swapcase() 方法Python String strip() 方法Python String translate() 方法Python String title() 方法Python String zfill() 方法Python String upper() 方法
Python Math 模块
Python Math exp() 函数Python Math ceil() 函数Python Math floor() 函数Python Math fabs() 函数Python Math log10() 函数Python Math log() 函数Python Math pow() 函数Python Math modf() 函数Python round() 函数Python Math sqrt() 函数Python Math acos() 函数Python Math asin() 函数Python Math atan() 函数Python Math atan2() 函数Python Math cos() 函数Python Math degrees() 函数Python Math hypot() 函数Python Math radians() 函数Python Math sin() 函数Python Math tan() 函数
Python Random 模块
Python random choice() 函数Python random random() 函数Python random randrange() 函数Python random seed() 函数Python random shuffle() 函数Python random uniform() 函数
Python List 模块
Python List min() 方法Python List len() 方法Python List list() 方法Python List max() 方法