盘点Python字符串常见的16种操作方法

开发 后端
本文详细的讲解了Python基础 ( 字符串 )。介绍了有关字符串,切片的操作。下标索引。以及在实际操作中会遇到的问题,提供了解决方案。希望可以帮助你更好的学习Python。

[[409765]]

大家好,我是Go进阶者,上篇文章给大家介绍了Python字符串,今天给大家分享一些Python字符串的常用操作,一起来看看吧~

一、常用操作

以字符串

  1. 'lstr = 'welcome to Beijing Museumitcpps fdsfs' 

为例,介绍字符常见的操作。

<1> find

检测 str 是否包含在 lstr中,如果是返回开始的索引值,否则返回-1。

语法:

  1. lstr.find(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2. print(lstr.find("Museum")) 
  3.  
  4. print(lstr.find("dada")) 

运行结果:

<2> index

跟find()方法一样,只不过如果str不在 lstr中会报一个异常。

语法:

  1. lstr.index(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2.  
  3. print(lstr.index("dada")) 

运行结果:

<3> count

返回 str在start和end之间 在 lstr里面出现的次数

语法:

  1. lstr.count(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.count("s")) 

运行结果:

<4> replace

把 lstr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

  1. 1str.replace(str1, str2,  1str.count(str1)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.replace("s""ttennd")) 

运行结果:

<5> split

以 str 为分隔符切片 lstr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

  1. 1str.split(str=" ", 2) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.split("to", 5)) 

运行结果:

<6> capitalize

把字符串的第一个字符大写。

  1. lstr.capitalize() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.capitalize()) 

运行结果:

<7> title

把字符串的每个单词首字母大写。

  1. >>> a = "hello itcast" 
  2. >>> a.title() 
  3. 'Hello Itcast' #运行结果 

<8> startswith

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

  1. 1str.startswith(obj) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.startswith('we')) 

运行结果:

<9> endswith

检查字符串是否以obj结束,如果是返回True,否则返回 False.

  1. 1str.endswith(obj) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.endswith('hfs')) 

运行结果:

<10> lower

转换 lstr 中所有大写字符为小写

  1. 1str.lower() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.lower()) 

运行结果:

<11> upper

转换 lstr 中的小写字母为大写

  1. 1str.upper() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.upper()) 

运行结果:

<12> strip

删除lstr字符串两端的空白字符。

  1. >>> a = "\n\t itcast \t\n" 
  2. >>> a.strip() 
  3. 'itcast'  #运行结果 

<13> rfind

类似于 find()函数,不过是从右边开始查找。

  1. 1str.rfind(str, start=0,end=len(1str) ) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rfind('eijing')) 

运行结果:

<14> rindex

类似于 index(),不过是从右边开始。

  1. 1str.rindex( str, start=0,end=len(1str)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rindex('eijing')) 

运行结果:

<15> partition

把lstr以str分割成三部分,str前,str和str后。

  1. 1str.partition(str) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.partition('eijing')) 

运行结果:

<16> join

mystr 中每个字符后面插入str,构造出一个新的字符串。

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. str='233' 
  3. lstr.join(str) 
  4. li=["my","name","is","LY"
  5. print(str.join(li)) 

运行结果:

二、总结

本文详细的讲解了Python基础 ( 字符串 )。介绍了有关字符串,切片的操作。下标索引。以及在实际操作中会遇到的问题,提供了解决方案。希望可以帮助你更好的学习Python。

 

责任编辑:姜华 来源: Go语言进阶学习
相关推荐

2010-03-11 09:56:57

Python字符串操作

2021-04-17 10:05:57

Python字符串Python基础

2020-06-28 08:26:41

Python开发工具

2015-04-08 10:27:43

JavaScript字符串操作函数

2023-04-17 19:23:10

字符串Bash

2009-08-28 15:25:38

C#线程操作

2010-02-01 09:40:08

Python操作

2010-02-01 16:22:36

Python字符串操作

2010-03-04 09:58:32

安装Python

2023-08-25 16:37:08

Pandas测试

2010-03-05 13:48:24

Python for

2019-12-02 09:24:10

Python数据字符串

2010-03-15 15:18:23

Python运行

2010-09-02 10:02:17

PHP

2020-08-01 16:19:13

JavaScript字符串开发

2023-12-05 08:02:51

JavaScript字符串功能

2021-05-18 09:08:18

字符串子串对象

2018-03-21 12:36:21

Python字符串

2024-03-12 07:35:39

Python字符串列表

2009-08-20 17:30:02

C#连接字符串
点赞
收藏

51CTO技术栈公众号