您的位置:首页 > 编程语言 > Python开发

Python学习笔记:字符串(str)有关函数

2016-05-09 13:24 676 查看
1.len()

len() 函数计算并返回字符串元素个数。(格式:len(string) )(string 代表字符串名字)

>>> S = 'abc'
>>> S
'abc'
>>> len(S)
3


2.max()、min()

max()、min()函数分别返回最大值和最小值,数字就返回数字,字母根据ASCII计算。(格式:max(string)、min(string) )

>>> S = 'abc'
>>> S
'abc'
>>> max(S)
'c'
>>> min(S)
'a'
>>> S = '123'
>>> S
'123'
>>> max(S)
'3'
>>> min(S)
'1'


3.enumerate()

enumerate() 函数用来遍历序列的坐标和元素。(格式:enumerate(string)


>>> S = 'abcd'
>>> S
'abcd'
>>> for i,s in enumerate(S):
print(i,s)

0 a
1 b
2 c
3 d
单纯返回的话不知道是什么东西。。。。

>>> S = 'abc'
>>> S
'abc'
>>> enumerate(S)
<enumerate object at 0x0000000003119168>


4.str()

str() 函数将参数转变为字符串类型并返回副本,不改变原有类型。(格式:str(string) )

>>> T = [1,2,3]
>>> T
[1, 2, 3]
>>> type(T)
<class 'list'>
>>> str(T)
'[1, 2, 3]'
>>> type(T)
<class 'list'>
>>> type(str(T))
<class 'str'>


5.chr() 、 ord()

chr() 函数可将传入的单个数字转化成对应的ASCII的字母返回。

ord() 函数可将传入的单个字母转化成对应的ASCII的数字返回。

>>> chr(65)
'A'
>>> ord('a')
97
>>> ord('abc')<span style="white-space:pre">		</span>#传入多个字母出现异常
Traceback (most recent call last):
  File "<pyshell#383>", line 1, in <module>
    ord('abc')
TypeError: ord() expected a character, but string of length 3 found


6.capitalize()
capitalize() 函数将字符串的第一个字母大写并返回副本,不改变原有数据。(格式:string.capitalize())

>>> S = 'abc'
>>> S
'abc'
>>> S.capitalize()
'Abc'
>>> S
'abc'


7.center()

center() 函数将使字符串居中输出,两边用空格填充,不改变原有数据。(格式:string.center(width) )(width为参数,表示总宽度 )

>>> S = 'abc'
>>> S
'abc'
>>> S.center(10)
'   abc    '		#宽度10
>>> S
'abc'
>>> S.center(2)
'abc'			#若设置的宽度小于元字符串,没什么变化。
>>> S
'abc'


8.count()

count() 函数用来返回str在string中出现的次数,如果beg或者end指定则返回指定范围内的str出现的次数。(格式:string.(str,beg=0,end=len(string)) )(len(string)代表字符串长度,下同,str代表单个字符,或者多个字符,beg代表开始位置,end代表结束位置)

>>> S = 'abcaaa'
>>> S
'abcaaa'
>>> S.count('a')
4
>>> S.count('a',2)	#统计位置2到最后,包含位置2
3
>>> S.count('a',0,3)	#由此可见不包含位置3,跟切片一样,包含前面的不包含后面的
1
>>> S.count('a',0,4)
2
>>> S.count('abc')<span style="white-space:pre">	#统计多个字符
1


9.endswith()、startswith()

endswith() 函数用来检查字符串是否以obj结束,如果beg或者end指定则检查指定范围内是否以obj结束,如果是返回True,反之False。

(格式:string.endswith(obj,beg=0,end=len(string)) )。

>>> S = 'abcabcd'
>>> S
'abcabcd'
>>> S.endswith('d')
True
>>> S.endswith('c')
False
>>> S.endswith('c',0,6)<span style="white-space:pre">	#不包含位置6,所以位置5是 'c '。
True


startswith() 函数用来检查字符串是否以obj开始,如果beg或者end指定则检查指定范围内是否以obj开始,如果是返回True,反之False。

(格式:string.startswith(obj,beg=0,end=len(string)) )。

>>> S = 'abcda'
>>> S
'abcda'
>>> S.startswith('a')
True
>>> S.startswith('a',1)
False


10.expandtabs()

expandtabs() 函数用来把字符串中的tab转换成空格输出。(格式:string.expandtabs(tabsize=8) )(tabsize代表一个tab转换成多少个空格,默认8)

>>> S = '\tabc'<span style="white-space:pre">	#\t  转义字符代表一个tab
>>> S
'\tabc'
>>> S.expandtabs()
'        abc'
>>> S = '\tabc'
>>> S
'\tabc'
>>> S.expandtabs(2)<span style="white-space:pre">	#设置一个tab转换2个空格
'  abc'
>>> S.expandtabs(1)<span style="white-space:pre">
</span>' abc'


11.find() 、rfind()

find() 函数用来查找string中是否存在str,如果存在则返回索引值,若有多个则返回第一个索引值,不存在则返回-1,如果beg或者end指定则查找指定范围内。

(格式:string.find(str,beg=0,end=len(string)) )。

>>> S = 'abcda'
>>> S
'abcda'
>>> S.find('a')
0
>>> S.find('a',1)	#从位置1开始,此时位置0的'a'不查找。
4
>>> S.find('ab')	#查找多个字符时,返回第一个字符的索引值
0
>>> S.find('bc')
1
>>> S.find('e')		#不存在返回-1
-1


rfind() 函数类似find()函数,不过是从右边开始查找的。(格式:string.rfind(str,beg=0,end=len(string)) )。

>>> S = 'abcda'
>>> S
'abcda'
>>> S.rfind('a')
4
>>> S.rfind('a',0,2)<span style="white-space:pre">
0
>>> S.rfind('a',1,2)<span style="white-space:pre">	#没有返回-1
-1


12.index()、rindex()

index() 函数跟find()函数功能一样,唯一不同的是查找的字符不存在时,则会出现异常。(格式:string.index(str,beg=0,end=len(string)) )

>>> S = 'abcd'
>>> S
'abcd'
>>> S.index('a')
0
>>> S.index('bc')
1
>>> S.index('e')
Traceback (most recent call last):
File "<pyshell#122>", line 1, in <module>
S.index('e')
ValueError: substring not found
>>> S.index('a',1)<span style="white-space:pre">	#从位置1开始查找,位置0的'a'不算其中。
Traceback (most recent call last):
File "<pyshell#123>", line 1, in <module>
S.index('a',1)
ValueError: substring not found


rindex() 函数类似于index() 函数不过是从右边开始的。(格式:string.rindex(str,beg=0,end=len(string)))

>>> S = 'abcda'
>>> S.rindex('a')
4
>>> S.rfind('a',1,2)
-1
>>> S.rindex('a',0,2)
0
>>> S.rindex('a',1,2)<span style="white-space:pre">		</span>#没有则会出现异常
Traceback (most recent call last):
File "<pyshell#198>", line 1, in <module>
S.rindex('a',1,2)
ValueError: substring not found


13.isalnum()、isalpha()

isalnum() 函数用来string如果至少有一个字符并且所有字符都是字母或者是数字,是就返回True,否则返回False。

isalpha() 函数与islnum() 函数类似,不过所有字符只能是字母,是数字的话也返回False。

(格式:string.isalnum()、string.isalpha() )

>>> S = 'abc'
>>> S
'abc'
>>> S.isalnum()
True
>>> S = '123'
>>> S
'123'
>>> S.isalnum()
True
>>> S = 'abc'
>>> S
'abc'
>>> S.isalpha()
True
>>> S = '123'
>>> S
'123'
>>> S.isalpha()
False
>>> S = ''
>>> S.isalnum()
False


14.isdigit()

isdigit() 函数用来检测字符串中是否值包含数字,是就返回True,否则返回False,空字符串也返回False。(格式:string.isdigit() )

>>> S = '123'
>>> S
'123'
>>> S.isdigit()
True
>>> S = '123a'
>>> S
'123a'
>>> S.isdigit()
False
>>> S = ''
>>> S
''
>>> S.isdigit()
False


15.isdecimal()

isdecimal() 函数用来检测字符串中是否只包含十进制数字,是就返回True,否则返回False.(格式:string.isdecimal() )

>>> S = '0x64'
>>> S
'0x64'
>>> S.isdecimal()
False
>>> S = '123'
>>> S
'123'
>>> S.isdecimal()
True


16.isspace()
isspace() 函数用来检测字符串是否只包含空格,是就返回True,否则返回False(格式:string.isspace() )

>>> S = '   '
>>> S
'   '
>>> S.isspace()
True
>>> S = ''
>>> S.isspace()
False


17.islower()、isupper()

islower() 函数用来检测string中是否至少包含一个区分大小写的字母,并且这些字母都是小写,是就返回True,否则返回False(格式:string.islower() )

>>> S = 'abc'
>>> S
'abc'
>>> S.islower()
True
>>> S = 'Abc'
>>> S.islower()
False
>>> S = '123'
>>> S
'123'
>>> S.islower()
False


isupper() 函数用来检测string中是否至少包含一个区分大小写的字母,并且这些字母都是大写,是就返回True,否则返回False(格式:string.isupper())

>>> S = 'ABC'
>>> S
'ABC'
>>> S.isupper()
True
>>> S = 'aBC'
>>> S
'aBC'
>>> S.isupper()
False
>>> S = '123'
>>> S.isupper()
False


18.rjust()、ljust()

rjust() 函数用来返回源字符串右对齐,并且用空格填充长度至width。(格式:string.rjust(width) )

>>> S = 'abc'
>>> S
'abc'
>>> S.rjust(10)
'       abc'
ljust() 函数用来返回源字符串左对齐,并且用空格填充长度至width。(格式:string.ljust(width) )

>>> S = 'abc'
>>> S
'abc'
>>> S.ljust(10)
'abc


19.lower()、upper()

lower() 函数将字符串中所有不是小写的字符转换成小写,不是字母的话没有变化。(格式:string.lower() )

upper() 函数将字符串中所有不是大写的字符转换成大写,不是字母的话没有变化。(格式:string.upper() )

>>> S = 'abcABC'
>>> S
'abcABC'
>>> S.lower()
'abcabc'
>>> S.upper()
'ABCABC'


20.swapcase()

swapcase() 函数用来反转字符中的大小写,不是字母的话没有变化。(格式;string.swapcase() )

>>> S = 'abcABC'
>>> S
'abcABC'
>>> S.swapcase()
'ABCabc'


21.rstrip()、lstrip()

rstrip() 函数用来截掉字符串右边的空格,不改变原有的数据。(格式:string.rstrip())

lstrip() 函数用来截掉字符串左边的空格,不改变原有的数据。(格式:string.lstrip() )

>>> S = '   abc   '
>>> S
'   abc   '
>>> S.rstrip()
'   abc'
>>> S.lstrip()
'abc   '
>>> S
'   abc   '


21.join()

join() 函数用seq中的元素(字符串)把string字符串重新组合起来称为一个新的字符串。(seq.join(string))

>>> S = 'abc'
>>> S
'abc'
>>> ':'.join(S)
'a:b:c'
>>> '...'.join(S)
'a...b...c'


22.replace()

replace() 函数用str2来替换原来的str1的内容,若有参数num的话,替换次数不超过num,num默认为string.count(str1),就是str1出现的所有次数。

(格式:string.replace(str1,str2,num=string.count(str1)) ))

>>> S = 'abcaaa'
>>> S
'abcaaa'
>>> S.replace('a','b')
'bbcbbb'
>>> S.replace('a','b',2)
'bbcbaa'


23.split()

split() 函数用str来分隔整个string并返回列表形式,如果有参数num的话,则分隔num次,num默认为string.count(str),就是str出现的所有次数。

(格式:string.split(str=' ',num=string.count(str)) )

>>> S = 'a:b:c:d'
>>> S
'a:b:c:d'
>>> S.split(':')
['a', 'b', 'c', 'd']
>>> S.split(':',2)
['a', 'b', 'c:d']


24.zfill()

zfill() 函数用来返回宽度为width的字符串,并且原字符串右对齐,前面用0填充空格。(格式:string.zfill(width) )

>>> S
'abc'
>>> S.zfill(10)
'0000000abc'


25.title()、istile()

title() 函数返回标题话的字符串,也就是每个单词的第一个字母大写。(格式:string.title() )

>>> S = 'abc def'
>>> S
'abc def'
>>> S.title()
'Abc Def'


istitle() 函数用来判断字符串是不是标题化,是就返回True,否则返回False(格式:string.istitle())

>>> S = 'abc def'
>>> S
'abc def'
>>> S.istitle()
False
>>> S = 'Abc Def'
>>> S
'Abc Def'
>>> S.istitle()
True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: