博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
partition拼字符串_Python字符串partition(),rpartition()
阅读量:2532 次
发布时间:2019-05-11

本文共 1774 字,大约阅读时间需要 5 分钟。

partition拼字符串

Python String partition() function splits a string based on a separator into a with three strings. The first string is the part before the separator, the second string is the separator and the third string is the part after the separator.

Python String partition()函数将基于分隔符的字符串拆分为具有三个字符串的 。 第一个字符串是分隔符之前的部分,第二个字符串是分隔符,第三个字符串是分隔符之后的部分。

Python字符串partition() (Python String partition())

This function syntax is:

该函数语法为:

str.partition(sep)

If the separator string is not found, then the 3-tuple contains the string itself followed by two empty strings.

如果找不到分隔符字符串,则三元组包含字符串本身,后跟两个空字符串。

Let’s look at some examples of partition() function.

让我们看一下partition()函数的一些示例。

s = 'Hello World 2019'parts_tuple = s.partition('World')print(parts_tuple)parts_tuple = s.partition('2018')print(parts_tuple)

Output:

输出:

('Hello ', 'World', ' 2019')('Hello World 2019', '', '')

Python字符串rpartition() (Python String rpartition())

Python String rpartition() splits the string at the last occurrence of the separator string. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.

Python字符串rpartition()在最后一次出现分隔符字符串时拆分字符串。 如果找不到分隔符,则返回一个包含两个空字符串的三元组,然后是字符串本身。

s = 'Hello World 2019'parts_tuple = s.rpartition('World')print(parts_tuple)parts_tuple = s.rpartition('2018')print(parts_tuple)

Output:

输出:

('Hello ', 'World', ' 2019')('', '', 'Hello World 2019')

Let’s look at an example where the difference between partition() and rpartition() function will be clear.

让我们看一个例子,其中partition()和rpartition()函数之间的区别将很明显。

s = 'ABCBA'parts_tuple = s.partition('B')print(parts_tuple)parts_tuple = s.rpartition('B')print(parts_tuple)

Output:

输出:

('A', 'B', 'CBA')('ABC', 'B', 'A')
. 签出更多Python示例。

Official Documentation:

官方文档:

翻译自:

partition拼字符串

转载地址:http://ymmzd.baihongyu.com/

你可能感兴趣的文章
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
在线教育工具—白板系统的迭代1——bug监控排查
查看>>
121. Best Time to Buy and Sell Stock
查看>>
hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
查看>>
安装php扩展
查看>>
百度移动搜索主要有如下几类结果构成
查看>>