partition拼字符串_Python字符串partition(),rpartition()
partition拼字符串
Python String partition() function splits a string based on a separator into a tuple 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')
Official Documentation: partition()
官方文档: partition()
翻译自: https://www.journaldev.com/24420/python-string-partition-rpartition
partition拼字符串
partition拼字符串_Python字符串partition(),rpartition()相关推荐
- python怎么显示字符串_python字符串
Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 ='Hello Wo ...
- python创建一个字符串_Python字符串基本操作
一.任务描述 本实验任务主要对Python字符串进行一些基本操作,通过完成本实验任务,要求学生熟练掌握Python字符串的基本操作,并对Python字符串基本操作进行整理并填写工作任务报告. 二.任务 ...
- python编写程序接收字符串_Python字符串操作
a = 'Hello' b = 'Python' 一.字符串运算符 1.字符串连接(+) '字符串1' + '字符串2' >>>print(a + b) HelloPython 2. ...
- python怎么创建字符串_Python 字符串
Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hel ...
- python横向输出字符串_Python字符串及用法详解
字符串是由数字.字母.下画线组成的一串字符,在编程语言中表示文本的数据类型.在 Python 2.X 中,普通字符串是以 8 位 ASCII 码进行存储的,而 Unicode 字符串则以 16 位 U ...
- [转载] python截取指定字符串_python字符串截取,python字符串切片的方法详解
参考链接: Python字符串| min 字符串本质上就是由多个字符组成的,Python 允许通过索引来操作字符,比如获取指定索引处的字符,获取指定字符在字符串中的位置等. Python 字符串直接在 ...
- python字符串_Python字符串
python字符串 Good day, learners. In this tutorial we are going to learn Python String. In our previous ...
- python 字节字符串_Python字符串转换为字节,字节转换为字符串
python 字节字符串 Python字符串到字节 (Python String to bytes) Python String to bytes conversion can be done in ...
- 以30字符宽居中输出python字符串_Python 字符串
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello World!'var2= ...
最新文章
- 多于2个字符串的拼接,禁止使用“+”,而应该用join
- win7网络的计算机名称,Win7指定的网络名不再可用快速解决教程
- windows环境下安装nodeJS和express,一直提示command not found-配置环境变量
- 查看mysql本地路径
- c语言与python通信_C和Python – 与套接字通信
- 【操作系统】页置换算法
- 前端技术面——(js基础二)
- 深刻理解HDFS工作机制
- C++ Coding Standard
- Spark源码分析之BlockStore
- MyEclipse_8.5+flex_4+Blazeds配置
- Redis应用场景一
- pythonzip压缩字符串_Python压缩与解压缩ZIP文件的实现方法
- PGM:图模型学习概述
- 数学建模入门-python实现单目标模糊综合评价法
- Red Giant Universe 3中文版
- 家庭养花的资料大全-春雷转
- Autofac 资料整理
- Oracle分区表概述、分类、使用方法及注意事项
- Power BI 参数解决源文件路径问题