字符串方法总结

1、strip()

描述:用于脱去字符串两边指定的字符(默认为空格)。

参数:chars  要脱去的子字符串

返回值:返回脱去子字符串后而生成的新字符串。

s = '    Hello World    '
print('原字符串>>>>',s,'<<<<')
s1 = s.strip()
print('更改后的字符串>>>>',s1,'<<<<<')
原字符串>>>>     Hello World     <<<<
更改后的字符串>>>> Hello World <<<<<
  
View Code

示例2

s = '*****Hello World*****'
print('原字符串>>>>',s,'<<<<')
s1 = s.strip('*')
print('更改后的字符串>>>>',s1,'<<<<<') 
原字符串>>>> *****Hello World***** <<<<
更改后的字符串>>>> Hello World <<<<<
View Code

3、count()

描述:用于统计字符串里指定字符出现的次数。

参数:sub  查询的子字符串

     start 开始索引。默认为第一个字符,第一个字符索引值为0

     end  结束索引。字符中第一个字符 的索引为0。默认字符串的最后一个位置

返回值:返回整型,指定的字符在字符串中出现的次数。

s = 'hello world'
s1 = s.count('l')
print(type(s1),s1)
<class 'int'> 3
View Code
s = 'hello world'
s1 = s.count('l',3,10) # 表示统计从索引位置3到9位置(顾首不顾尾)的子串的个数
print(type(s1),s1)
<class 'int'>   2
View Code

4、split()

描述:分隔字符串

参数:sep  分隔符

     maxsplit  最多分隔的次数

返回值:返回分割后的字符串列表

s = 'hello world'
s1 = s.split()    # 括号内什么不加默认以空格为分隔符
print(type(s1),s1)# 得到一个列表
<class 'list'> ['hello', 'world']
View Code

5、replace()

描述:子串替换

参数:old  要替换的字符串

     new  新的字符串

     count  替换次数

返回值:去掉字符串中的旧字符串,替换成新字符串后生成一个新的字符串。

s = 'hello world'
s1 = s.replace('world','python')
print(type(s1),s1)
<class 'str'> hello python
View Code
s = 'hello world world'
s1 = s.replace('world','python',1)  # 只替换第一个world
print(type(s1),s1)
<class 'str'> hello python world
View Code

6、index()(空格也占位)

描述:查找字符串,返回子串的索引,使用方法与find相同,不同之处是若查找的子串不存在会报错

参数:sub  查找的子字符串

     start  查找的起始位置

     end  查找的结束位置

返回值:若包含子字符串则返回开始的索引值,否则抛出异常。

s = 'hello world'
s1 = s.index('world') # 如果要查找的子字符串没有,则会抛出异常。
print(type(s1),s1)
<class 'int'> 6
View Code
s = 'hello world'
s1 = s.index('o',5,10) # 表示从索引5至索引10的位置的开始查找字符“o“的所在的索引位置。
print(type(s1),s1)
<class 'int'> 7
View Code

7、capitalize()

描述:将字符串的首字母大写

参数:无

返回值:返回一个新的首字母大写的字符串对象。

s = 'hello world'
s1 = s.capitalize()  # 将s字符串的首字母大字
print(type(s1),s1)
<class 'str'> Hello world
View Code

8、center()

描述:让字符串居中对齐

参数:width  宽度,返回字符串长度

   fillchar  填充字符,不填默认以空格为填充字符

返回值:返回新的居中对齐的字符串

s = 'hello world'
s1 = s.center(26)   # 字符串在26个空格中间
print(type(s1),'起始位置',s1,'结束位置')
<class 'str'> 起始位置        hello world         结束位置
View Code
s = 'hello world'
s1 = s.center(26,'#')  # 字符串在26个"#"号中间
print(type(s1),s1)
<class 'str'> #######hello world########
View Code

10、endswith()

描述:判断是否以指定的子串结尾

参数:suffix  子串

   start  开始索引

   end  结束索引  

返回值: 返回布尔值

s = 'hello world'
s1 = s.endswith('rld')
print(type(s1),s1)
<class 'bool'> True
View Code
s = 'hello world'
s1 = s.endswith('or',1,9) # 表示从索引位置1至8(顾首不顾尾)查找是否以字符'or'结尾
print(type(s1),s1)
<class 'bool'> True
View Code

12、find()

描述:检测字符串中是否包含指定的子串

参数:sub  要查找的子串

   start 查找的开始位置

   end  查找的结束位置

返回值:若包含子串则返回开始的索引值,否则返回-1

s = 'hello world'
s1 = s.find('l',5,10) # 查找索引5至9位置中的字符“l”的位置
print(type(s1),s1)
<class 'int'> 9
View Code
s = 'hello world'
s1 = s.find('x') # 查找一个字符串中不存在的子串
print(type(s1),s1)
<class 'int'> -1
View Code

13、format()

描述:字符串的格式化输出。

种类:分别是%和{} 两种占位符格式控制符

数据格式类型:

(1)%%  百分号标记

(2) %c   字符及其ASCII码

(3) %s  字符串                重点

(4)%d  有符号整数(十进制)        重点

(5)%u  无符号整数(十进制)

(6)%o  无符号整数(八进制)

(7)%x  无符号整数(十六进制)

(8)%X  无符号整数(十六进制大写字符)

(9)%e  浮点数(科学计数法)

(10)%E  浮点数(科学计数法,用E代替e)

(11)%f  浮点数(用小数点符号)         重点

(12)%g  浮点数(根据值的大小采用%e或%f)

(13)%G  浮点数(类似于%g)

(14)%p  指针(用十六进制打印值的内存地址)

(15)%n  存储输出字符的数量放进参数列表的下一个变量中

参数:*args,**kwargs表示接收所有传参。

返回值:返回一个新的字符串。

print('my name is %s my age is %s my height is %s'%('alex',45,178))
my name is alex my age is 45 my height is 178
View Code
print('my name is {name} my age is {age} my height is {height}'.format(name='alex',age=35,height=178))
my name is alex my age is 35 my height is 178
View Code

16、isalpha()

描述:判断一个字符串是否全是字母

参数:无

返回值:返回一个布尔值

s = '88888hello world88888'
s1 = s.isalpha()
print(type(s1),s1)
 
s2 = 'helloworld'
s3 = s2.isalpha()
print(type(s3),s3)
<class 'bool'> False
<class 'bool'> True
View Code

18、isdigit()

描述:判断一个字符串中是否由数字组成

参数:无

返回值:返回一个布尔值(如果由数字组成则返回True,否则返回False)

s = '123456789'
s1 = s.isdigit()
print(type(s1),s1)
<class 'bool'> True

23、istitle()

描述:判断字符串中的每个单词首字母是否为大写

参数:无

返回值:返回一个布尔值(字符串中的每个单词首字母为大写则返回True,否则返回False)

s = '888888What Is Your Name?'
s1 = s.istitle()
print(type(s1),s1)
 
s2 = '888888what is your name?'
s3 = s2.istitle()
print(type(s3),s3)
<class 'bool'> True
<class 'bool'> False
View Code

25、join()

描述:将序列中的元素以指定的字符连接,生成一个新的字符串。

参数:iterable  指定的字符

返回值:返回通过指定字符连接序列中元素后生成的新字符串

s1 = '-'
s2 = ''
seq = ('h','e','l','l','o')
print(s1.join(seq))
print(s2.join(seq))
h-e-l-l-o
hello
View Code

27、lower()

描述:将字符串中的大写字母替换成小写

参数:无

返回值:返回一个小写的字符串

s = 'HELLO WORLD'
s1 = s.lower()
print(type(s1),s1)
<class 'str'> hello world
View Code

31、rfind()

描述:返回字符串最后一次出现的位置,如果没有匹配项则返回-1

参数:sub  查找的字符串

   start  开始查找的位置(默认为0)

   end  结束查找位置(默认为字符串的长度)

返回值:返回字符串最后一次出现的位置,如果没有匹配则返回-1

s = 'Alex is a big SB'
s1 = 'is'
print(type(s.rfind(s1)),s.rfind(s1))
<class 'int'> 5
View Code

32、rindex()

描述:返回子串在字符串中最后出现的位置,如果没有匹配的字符串会报异常

参数:sub  查找的字符串

   start  开始位置

   end  结束位置

返回值:返回子串在字符串中最后出现的位置,如果没有匹配的字符串会报异常

s = 'Alex is a big SB'
s1 = s.rindex('i')
print(type(s1),s1)
<class 'int'> 11
View Code

42、upper()

描述:将字符串中的小写字母转为大写字母

参数:无

返回值:返回小写字母转为大写字母后的字符串

洋洋人生如戏一场,芸芸众生各属一角。此戏说长便长,论时百年;说长便短,稍纵即逝。生命沧海一粟,在万物中脆弱瞬间即逝为最,随风而来,随风为土,虽说百年,亦不过宇宙间流星划过夜空,一闪而过,无迹无痕,虽曾有闪烁,但即刻消失的无影无踪。无论此戏是悲是喜。 人生轻松无极限,生活快乐遂心愿,活的好过的开心,心态代表一个人的精神状态,只要有良好的心态,你才能每天保持饱满的心情。心态好,运气就好。精神打起来,好运自然来。记住做任何事情一定要有积极的心态,一旦失去他,就跳出去,要学会调整心态, 有良好的心态工作。 人生过的是心情,生活活的是心态,人生随其然,生活何其烦,累了就睡觉,醒了就微笑,走过一些路,才知道辛苦;登过一些山,才知道艰难;趟过一些河,才知道跋涉;跨过一些坎,才知道超越;经过一些事,才知道经验;阅过一些人,才知道历练;读过一些书,才知道财富。
原文地址:https://www.cnblogs.com/liuchengdong/p/7227960.html