字符串就是一串字符,是编程语言中表示文本的数据类型。
在Python中可以使用一对双引号或一对单引号定义一个字符串。
Python针对字符串一共有44个方法。
1.len方法用于统计字符串的长度
str = 'hello world!' print(len(str)) >>> 12
# 空格也算
2.count统计某个字符在字符串中出现的次数,或在字符串指定区间内完成上述操作。
str = 'hello world!' print(str.count('l')) >>> 3
如果指定的小字符串(子字符串)没有出现在大字符串中,则返回0
str = 'hello world!' print(str.count('x')) >>> 0
从索引值0-6范围的字符中统计l出现的次数
str = 'hello world!' print(str.count('l', 0, 6)) >>> 2
3.index方法用于从字符串中找出某个对象第一个匹配的索引位置,如果这个对象不再其中会报一个异常。
str = 'hello world!' print(str.index('l'))
>>> 2
# 索引位置从0开始
4.find方法可以在一个较长的字符串中查找子串,它返回子串所在位置的最左端索引。如果没有找到,则返回 -1。
url = 'www.google.cn' print(url.find('cn')) print(url.find('com')) >>> 11 >>> -1
#find和index方法类似,相同点是如果查找的字符在字符串中,那么都会返回字符串中最左端的索引位置,不同点是如果字符不在字符串中,find返回-1。index则会抛出valueError异常。
5.rfind方法用来查询指定字符最后位置的索引。如果没有找到,则返回 -1。
info = 'my name is ... and i am ... old' print(info.rfind('ol')) print(info.rfind('abc')) >>> 28 >>> -1
#最后一个字符'ol'的索引起始位置是28
6.strip方法返回去除两侧(不包括内部)空格的字符串。
info = ' my name is xxx and i am xxx old ! ' print(info.strip()) >>>my name is xxx and i am xxx old !
#strip()括号内无空格
也可以指定需要去除的字符,将他们列为参数即可(这个方法只删除开头或末尾的指定字符,因此中间的不会被删除)
info = '!my name is xxx! and i am xxx old !' print(info.strip('!')) >>> my name is xxx! and i am xxx old
#如果字符串'!'前后有空格,则无效。
7.lstrip/rstrip去除字符串左侧/右侧的空格或指定的字符。
info = '!my name is xxx! and i am xxx old !' print(info.lstrip('!')) print(info.rstrip('!')) >>> my name is xxx! and i am xxx old ! >>> !my name is xxx! and i am xxx old
8.join方法用来连接序列中的元素。
x = '...' color = ('hong', 'huang', 'lan') print(x.join(color)) >>> hong...huang...lan
#用字符串(这里是'...')替代掉列表中的逗号(x必须是字符串),返回的是全新的字符串。可以将返回的值赋给另外一个变量。
x = '...' color =['hong', 'huang', 'lan'] a = x.join(color) print(x, type(x)) >>>... <class 'str'> >>>print(a, type(a)) hong...huang...lan <class 'str'> print(color, type(color)) ('hong', 'huang', 'lan') <class 'list'>
#列表没有join方法,join方法只对字符串生效(列表/元组的元素必须都是字符串),对列表/元组进行操作,返回字符串。
还有一种直接用指定的字符串连接元素
color =['hong', 'huang', 'lan'] print('2'.join(color))、 >>>hong2huang2lan
9.split方法通过制定分割符对字符串进行切片,是join的逆方法
url = 'www.google.com' print(url.split('.')) >>>['www', 'google', 'com']
#使用句点来替换掉字符串中指定的内容,返回的是个新列表,原来的字符串不变,split只对字符串有效。
10.title方法将每个单词的首字母都改为大写
url = 'www.google.com' print(url.title()) >>> Www.Google.Com url = 'www google com' print(url.title()) >>>Www Google Com url = 'www*google*com' print(url.title()) >>>Www*Google*Com url = 'www2google3com' print(url.title()) >>>Www2Google3Com url = 'wwwgooglecom' print(url.title()) >>>Wwwgooglecom
#根据特殊字符判断?
11.caplitalize方法用来将字符串小写首字母转换成大写
str = 'hello world!' print(str.capitalize()) >>> Hello world!
# 仅限于字符串第一个字母,除字母之外(数字,特殊字符)无效。
12.upper,lower方法将字符串中大/小写转换成大/小写
str = '_hello world!' print(str.upper()) >>>_HELLO WORLD! str = '_Hello World!' print(str.lower()) >>>_hello world!
13.center方法填充字符串长度,使用原有字符串加填充字符构成指定长度的新的字符串
str = 'hello world' print(str.center(20)) print(str.center(20, '-')) >>> hello world >>>----hello world-----
14.ljust,rjust方法用于指定扩展字符串长度。
str = 'hello world' print(str.ljust(5)) print(str.ljust(20, '*')) >>>hello world >>>hello world*********
str = 'hello world'
print(str.rjust(5))
print(str.rjust(20, '*'))
>>>hello world
>>>*********hello world
#指定长度小于字符串长度则无效,如没有第二个参数,则默认使用空格代替。
15.replace把字符串中的旧字符替换成新字符,如果指定第三个参数,则替换不超过3次
str = 'this is a test' print(str.replace('is', 'xxx')) >>>thxxx xxx a test
可以指定次数(第三个参数)
str = "this is string example....wow!!! this is really string" print(str.replace('is', 'was', 3)) >>> thwas was string example....wow!!! thwas is really string