Python3笔记--字符串

it2024-10-11  36

什么是字符串?

是以单引号或双引号括起来的任意文本如:

string1 = 'abc'string2 = "cde"

 

字符串是不可变的,即以下尝试修改字符串的做法是错误的:

string = 'admin' string[0] = 'b'#会报出异常--TypeError: 'str' object does not support item assignment

 

字符串的方法

eval()--它可以将字符串str当成有效的表达式来求值并返回计算结果 print(eval('-135'))#-135print(eval("12+3"))#15print(eval("12-3"))#9print(eval("{'key':'value'}"))#此处返回一个字典{'key':'value'}

 

len()--返回字符串的长度 print(len("sunck is a good man!"))#20

 

lower()--转换字符串中大写字母为小写字母 string="SUNCK is A Good Man" print('string=%s'% string.lower())#string=sunck is a good man

 

upper()--将字符串所有小写字母转换成大写字母 string="SuNck Is a gOOd man" print(string.upper())#SUNCK IS A GOOD MAN

 

 swapcase()--转换字符串中字母的大小写 string="SuNck Is a gOOd man" print(string.swapcase())#sUnCK iS A GooD MAN

 

 capitalize()--最首字母大写,其他小写 string="SuNck Is a gOOd man" print(string.capitalize())#Sunck is a good man

 

 title()--每个单词首字母大写 string="SuNck Is a gOOd man" print(string.title())#Sunck Is A Good Man

 

center(width,fillchar)--字符串居中两边填充fillchar(默认空格)宽为width string="SuNck Is a gOOd man" print(string.center(40,"*"))#**********SuNck Is a gOOd man***********

 

  ljust(width[,fillchar])--返回一个指定宽度的左对齐字符串,fillchar为填充字符,填在右边 string="SuNck Is a gOOd man" print(string.ljust(40,"%"))#SuNck Is a gOOd man%%%%%%%%%%%%%%%%%%%%%

 

rjust(width[,fillchar])--返回一个指定宽度的有=右对齐字符串,fillchar为填充字符,填在左边 string='you are nice man' print(string.rjust(40,'*'))#************************you are nice man

 

zfill(width)--返回一个长度为width的字符串,原字符串右对齐,前面补0 string='she was beautiful' print(string.zfill(40)) #00000000000000000000000she was beautiful

 

 

 count(str[,start][,end])--返回字符串中str出现次数,可指定范围 string='he is very very handsome' print(string.count('very')) #2

 

 find(str[,start][,end])--检测str字符串是否在字符串中,返回第一次出现的开始下标,没有返回-1 string='he is very very handsome' print(string.find('very'))#6

 

 rfind(str[,start][,end])--从右检测str字符串是否在字符串中,返回第一次出现的开始下标,没有返回-1 string='he is very very handsome' print(string.rfind('very')) #11

 

 index(str,start=0,end=len(str))--跟find()一样,只不过如果str不存在会返回异常 string='kaige is a very very nice man' print(string.index('very')) #11

 

 rindex(str,start=0,end=len(str))--跟index()一样,只不过从右开始检索 string='kaige is a very very nice man' print(string.rindex('very')) #16

 

lstrip()--截掉字符串左侧指定的字符,默认为空格 string='******kaige is a very nice man' print(string.lstrip('*')) #kaige is a very nice man  rstrip()--截掉字符串右侧指定的字符,默认为空格 string='******kaige is a very nice man*****' print(string.rstrip('*')) #******kaige is a very nice man

 

strip(str)--截去字符串两边str string='********kaige is a very nice man******' print(string.strip('*') #kaige is a very nice man

 

 

split(str='',num)--以str为分隔符截取字符串,指定截num个。 string="sunck**is*****a***good*man" list_=string.split("*") print(list_)#['sunck', '', 'is', '', '', '', '', 'a', '', '', 'good', 'man']

 

 

splitlines([keepends])--按照(‘\r’,‘\r\n’,‘\n’)分隔,keepends=True ,会保留换行符。默认为False string='''sunck is a good man! sunck is a handsomoe man! sunck is a nice man! ''' print(string.splitlines()) #['sunck is a good man!', 'sunck is a handsomoe man!', 'sunck is a nice man!']

 

join(seq)--以指定的字符串分隔符,将seq中的所有元素组合 list_=['sunck','is','a','good','man'] str2="*".join(list_) print(str2) #sunck*is*a*good*man

 

max()、min()--它们作用于字符串是以ascii码表顺序比较大小 max('a','A') #'a'

 

 

replace(oldstr,newstr,count)--用newstr替换oldstr,默认是全部替换。如果指定了count,那么就替换前count个 string="sunck is a good good good man" string=string.replace("good","nice",1) print(string) #sunck is a nice good good man

 

 

startswith(str,start=0,end=len(str))--判断在给定的范围内是否以给定的字符串开头,如果没有给定范围, 则默认整个字符串 string="sunck is a nice man" print(string.startswith('sunck'))#True print(string.startswith("sunck",5,16)#False

 

endswith(str,start=0,end=len(str))--判断在给定的范围内是否以给定的字符串结尾,如果没有给定范围, 则默认整个字符串 string="sunck is a great man" print(string.endswith("man")) #True

 

encode(encoding="utf-8",errors="strict")--编码 string="sunck is a good man" data=string.encode("utf-8") print(data) #b'sunck is a good man'

 

decode()--解码 注意:要与编码的格式一致 string="sunck is a good man" data=string.encode("utf-8") string=data.decode("utf-8") print(string) #sunck is a good man

 

isalpha()--如果字符串中至少有一个字符,且所有字符都是字母返回True,否则为False string="sunckisagoodman" print(string.isalpha()) #True

 

isalnum()--如果字符串中至少有一个字符,且所有字符都是字母或数字返回True,否则返回False string="1a2b3" print(string.isalnum()) #True

 

isupper()--如果字符串字至少有一个英文字符且所有英文字符都是大写返回True,否则为False print("AB#C".isupper())#True print("1".isupper())#False

 

islower()--如果字符串字至少有一个英文字符且所有英文字符都是小写返回True,否则为False print("abcA".islower())#False print("1".islower())#False print("abc".islower())#True  istitle()--如果字符串是标题化的返回True,否则返回False print("Sunck Is".istitle())#True print("Sunck is".istitle())#False

 

isdigit()--如果字符串中只包含数字字符返回True,否则返回False print("123".isdigit())#True print("123a".isdigit())#False

 

isnumeric()--同上 print("123".isnumeric())#True print("123a".isnumeric())#True

 

isdecimal()--字符串只包含十进制字符 print("123".isdecimal())#True print("123a".isdecimal())#False

 

isspace()--如果字符只包含空格返回True,否则返回False print(" ".isspace())#True print(" \t".isspace())#True

 

 

转载于:https://www.cnblogs.com/byadmin/articles/foundation-1.html

最新回复(0)