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')
GitHub Repository.GitHub存储库中签出更多Python示例。

Official Documentation: partition()

官方文档: partition()

翻译自: https://www.journaldev.com/24420/python-string-partition-rpartition

partition拼字符串

partition拼字符串_Python字符串partition(),rpartition()相关推荐

  1. python怎么显示字符串_python字符串

    Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 ='Hello Wo ...

  2. python创建一个字符串_Python字符串基本操作

    一.任务描述 本实验任务主要对Python字符串进行一些基本操作,通过完成本实验任务,要求学生熟练掌握Python字符串的基本操作,并对Python字符串基本操作进行整理并填写工作任务报告. 二.任务 ...

  3. python编写程序接收字符串_Python字符串操作

    a = 'Hello' b = 'Python' 一.字符串运算符 1.字符串连接(+) '字符串1' + '字符串2' >>>print(a + b) HelloPython 2. ...

  4. python怎么创建字符串_Python 字符串

    Python 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hel ...

  5. python横向输出字符串_Python字符串及用法详解

    字符串是由数字.字母.下画线组成的一串字符,在编程语言中表示文本的数据类型.在 Python 2.X 中,普通字符串是以 8 位 ASCII 码进行存储的,而 Unicode 字符串则以 16 位 U ...

  6. [转载] python截取指定字符串_python字符串截取,python字符串切片的方法详解

    参考链接: Python字符串| min 字符串本质上就是由多个字符组成的,Python 允许通过索引来操作字符,比如获取指定索引处的字符,获取指定字符在字符串中的位置等. Python 字符串直接在 ...

  7. python字符串_Python字符串

    python字符串 Good day, learners. In this tutorial we are going to learn Python String. In our previous ...

  8. python 字节字符串_Python字符串转换为字节,字节转换为字符串

    python 字节字符串 Python字符串到字节 (Python String to bytes) Python String to bytes conversion can be done in ...

  9. 以30字符宽居中输出python字符串_Python 字符串

    字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello World!'var2= ...

最新文章

  1. Discuz在线升级中的SC和TC分别是什么意思?
  2. Android 2018最新验证手机号正则表达式
  3. 快速配置Ehcache
  4. 计算机导论结业报告大一,河北工业大学计算机导论结业论文.pdf
  5. git 使用分支基础
  6. 收款码合并有什么弊端_为什么明明有个人收款码了,还要申请微信收款商业版?...
  7. oracle buffercache,Oracle DBA开发之:使用sql来查看buffer cache的使用情况
  8. classes是什么意思怎么读_classes怎么读用法大全_classes是什么意思
  9. JavaWeb项目框架
  10. 生命游戏(康威的生命游戏) Game Of Life - Conway Cocos Creator 3.x版实现
  11. 2年工作经验杂谈(java开发)
  12. 【记录】好用的字体识别工具(网站)
  13. 【今日爆点】华为HDC开发者大会上正式发布深度欧拉V1.0
  14. 大学女生兼职适合做的工作有哪些?这些总有一样适合你
  15. Win11安装Ubuntu子系统报错WslRegisterDistribution failed with error: 0x800701bc
  16. 最全的日期年农历假日节气星座等计算类封装
  17. L1-009 N个数求和 (20分)(分数求和)
  18. 数字图像处理Python实战--高斯拉普拉斯金字塔图像重建
  19. java毕业生设计专利查询与发布系统设计与实现计算机源码+系统+mysql+调试部署+lw
  20. 监听微信手Q的挂起事件(visibilitychange)

热门文章

  1. 常用的各种标准下载网站(HB GB GJB MH)
  2. Oracle增加自增长列
  3. VB MSFlexGrid控件使用问题
  4. 不能不说的C#特性-表达式树
  5. 开发易于移植的J2ME游戏
  6. Shut Down(or Closing) Your Windows and Open The Unix
  7. [转载] python四种列表的插入方法及其效率
  8. 关于verilog的一些基础知识整理
  9. IntelliJ IDEA(三、各种工程的创建 -- 之二 -- 创建一个JavaWeb工程)
  10. 寒假小软件开发记录02--布局