partition

概要Description

签名

s.partition(sep: str): (left: str, sep: str, right: str) | (origin: str, '', '')

功能

在 s 中 从左往右 找到 第一个 sep 出现的位置,使用 sep 切割 s,

返回 切割后 左边的 left,切割符 sep 和 右边的 right 组成的 元祖 tuple

如果没有找到 sep,则 返回 切割前的 s 和 两个 空字符串 组成的 元祖 tuple

返回后,原字符串 s 在调用后保持不变

广告

案例Examples

正常情况

出现一次
1 2 =
s = 'page.html'result = s.partition('.')('page', '.', 'html')
sep 出现多次

选最左边的

1 2 =
s = './page.html'result = s.partition('.')('', '.', '/page.html')
没有出现
1 2 =
s = '/'result = s.partition('.')('/', '', '')
广告

ZZAX 微信公众

文档一更新,立刻告诉你