Python中关于字符串的方法详解(一)

1.方法目录

方法名称 功能
s.count(str) 返回 str 在 s 里面出现的次数
s.index(str[,start,end]) 返回子串在字符串中的索引,不存在则抛异
s.find(str[,start,end]) 返回指定字符串 str 在 s 中的索引,不存在返回 -1
s.replace(old,new[,count]) 用 new 替换在 s 中发现 old 子串,默认全部替换
s.strip([substr]) 去除 s 字符串两端的空格、转义字符或指定的字符 substr
s.startswith(substr) 判断字符串 s 是否以 substr 开头,是则返回 True
s.endswith(substr) 判断字符串 s 是否以 substr 结尾,是则返回 True
s.split(str[,count]) 将字符串用指定的字符串进行分割,去除指定的字符串并返回列表类型
s.join(iterable) 将字符串 s 去连接序列中的各个元素,返回 str 类型
s.rjust(width[,fillchar]) 返回一个原字符串右对齐,并使用 fillchar 填充
s.ljust(width[,fillchar]) 返回一个原字符串左对齐,并使用 fillchar 填充
s.zfill(width) 返回长度为 width 的字符串,原字符串右对齐,前面填充0
s.center(width[,fillchar]) 返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格
s.expandtabs(tabsize=8) 把字符串 s 中的 tab 符号转为空格,tab 符号默认的空格数是8

2.方法详解

(1)s.count(str)

  • 功能:返回str在s里面出现的次数(str可以是单个或多个字符)
"uaisaiouua".count("i")    
"uaisaiouua".count("u")
"uaisaiouua".count("ai")

输出结果为:

2
3
2

(2)s.index(str[,start,end])

  • 功能:返回子串在字符串中的索引,不存在则抛异
"uaisaiouua".index("a")
"uaisaiouua".index("a", 2)
"uaisaiouua".index("a", 2, -2)

"uaisaiouua".index("y")     #若子串不存在,则报错

输出结果为:

1
4
4

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

(3)s.find(str[,start,end])

  • 功能:返回指定字符串str在s中的索引,不存在返回 -1
"uaisaiouua".find("u")
"uaisaiouua".find("u", 2)
"uaisaiouua".find("u", 2, -2)

"uaisaiouua".find("h")     #若子串不存在,则返回-1

输出结果为:

0
7
7

-1

(4)s.replace(old,new[,count])

  • 功能:用new替换在s中发现old子串,默认全部替换
"uaisaiouua".replace("u", "l")     #count若不指定,默认为全部替换
"uaisaiouua".replace("u", "l", 2)  #count指定为2,则仅替换前两个"u"

输出结果为:

'laisaiolla'
'laisaiolua'

(5)s.strip([substr])

  • 功能:去除s字符串两端的空格、转义字符或指定的字符substr
" asidiha\n".strip()       #默认去除str字符串两端的空格、转义字符
"wasidihae".strip("ew")    #"ew"为一集合,表示去除字符串两端的"e"、"w"或"w"与"e"的组合
"wasidihaewe".strip("ew")  

"pasidihap".rstrip("p")    #只去除右边的
"pasidihap".lstrip("p")    #只去除左边的

输出结果为:

'asidiha'
'asidiha'
'asidiha'

'pasidiha'
'asidihap'

(6)s.startswith(substr)

  • 功能:判断字符串s是否以substr开头,是则返回True
"wwygiusgr".startswith("ww")

"wwygiusgr".startswith("g")         
"wwygiusgr".startswith("g", 3, -2)   #可以对片段进行操作,检测字符串中的片段是否以substr开头

输出结果为:

True

False
True

(7)s.endswith(substr)

  • 功能:判断字符串s是否以substr结尾,是则返回True
"wwygiusgr".endswith("r")

"wwygiusgr".endswith("g")         
"wwygiusgr".endswith("g", 3, -1)   #可以对片段进行操作,检测字符串中的片段是否以substr结尾

输出结果为:

True

False
True

(8)s.split(str[,count])

  • 功能:将字符串用指定的字符串进行分割,去除指定的字符串并返回列表类型
"awdjasjkllm".split("j")
"awdjasjkllm".split("j", 1)   #count=1,代表只分割一次

输出结果为:

['awd', 'as', 'kllm']
['awd', 'asjkllm']

(9)s.join(iterable)

  • 功能:用字符串s去连接序列中的各个元素,返回str类型
  • iterable是一个可迭代类型,如元组、列表和字符串
"".join(['awd', 'as', 'kllm'])
"#".join(['awd', 'as', 'kllm'])

输出结果为:

'awdaskllm'
'awd#as#kllm'

(10)s.rjust(width[,fillchar])

  • 功能:返回一个原字符串右对齐,并使用fillchar填充
"fada".rjust(10)       #fillchar默认为空格
"fada".rjust(10, "*")

输出结果为:

'      fada'
'******fada'

(11)s.ljust(width[,fillchar])

  • 功能:返回一个原字符串左对齐,并使用fillchar填充
"fada".ljust(10)       #fillchar默认为空格
"fada".ljust(10, "+")

输出结果为:

'fada      '
'fada++++++'

(12)s.zfill(width)

  • 功能:返回长度为width的字符串,原字符串右对齐,前面填充0
"ada".zfill(10)

输出结果为:

'0000000ada'

(13)s.center(width[,fillchar])

  • 功能:返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格
"asydi".center(10)        #fillchar默认为空格
"asydi".center(10, "#")

输出结果为:

'  asydi   '
'##asydi###'

(14)s.expandtabs(tabsize=8)

  • 功能:把字符串s中的 tab 符号转为空格,tab 符号默认的空格数是8
  • 对于字符串中直接敲入的tab无效,只对字符串中的转义字符(\t)有效
"fas\tdde".expandtabs()     #tab 符号默认的空格数是8
"fas\tdde".expandtabs(16)

输出结果为:

'fas     dde'
'fas             dde'

猜你喜欢

转载自blog.csdn.net/Zachary_H/article/details/106017214