首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python中字符串惯用的方法

2013-12-28 
Python中字符串常用的方法str this is string example....wow!!!print str.capitalize() : , str.c

Python中字符串常用的方法
str = "this is string example....wow!!!";print "str.capitalize() : ", str.capitalize()#output resultstr.capitalize() : This is string example....wow!!!

str = "this is string example....wow!!!";sub = "i";print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)sub = "wow";print "str.count(sub) : ", str.count(sub)

?

#output resultstr.count(sub, 4, 40) :  2str.count(sub, 4, 40) :  1

?

?str = "this is string example....wow!!!";suffix = "wow!!!";print str.endswith(suffix);print str.endswith(suffix,20);suffix = "is";print str.endswith(suffix, 2, 4);print str.endswith(suffix, 2, 6);#output resultTrueTrueTrueFalse

str1 = "this is string example....wow!!!";str2 = "exam";print str1.find(str2);print str1.find(str2, 10);print str1.find(str2, 40);#result1515-1

str = "this is string example....wow!!!";str = "exam";print str.index(str);print str.index(str, 10);print str.index(str, 40);#result1515Traceback (most recent call last): File "test.py", line 8, in print str.index(str, 40);ValueError: substring not found?isalnum()判断字符串是否全是字母和数字(要么全是字母,要么全是数字,要么全是数字和字母)例如:

?

str = "this2009";  # No space in this stringprint str.isalnum();str = "this is string example....wow!!!";print str.isalnum();#resultTrueFalse
?isalpha()方法判断字符串内容全是字母。例如:

?

str = "this";  # No space & digit in this stringprint str.isalpha();str = "this is string example....wow!!!";print str.isalpha();#resultTrueFalse
?isdecimal()和isnumeric()判断字符串是否全是数字,该字符串必须是unicode object。例如:

?

str = u"this2009";  print str.isdecimal();str = u"23443434";print str.isdecimal();#resultFalseTrue
?str = "123456"; # Only digit in this stringprint str.isdigit();str = "this is string example....wow!!!";print str.isdigit();#resultTrueFalse?islower()判断字符串中所有的字母是否都是小写。str = "THIS is string example....wow!!!"; print str.islower();str = "this is string example....wow!!!";print str.islower();#resultFalseTrue?

str = " \t\n"; #include tab,spaceprint str.isspace();str = "This is string example....wow!!!";print str.isspace();#resultTrueFalse

?istitle()判断字符串中,每个单词的首字母是否都是大写。例如:

?

str = "This Is String Example...Wow!!!";print str.istitle();str = "This is string example....wow!!!";print str.istitle();#resultTrueFalse

?str = "-";seq = ("a", "b", "c"); # This is sequence of strings.print str.join( seq );#resulta-b-c

len(str)str = "this is string example....wow!!!";print str.swapcase();str = "THIS IS STRING EXAMPLE....WOW!!!";print str.swapcase();#resultTHIS IS STRING EXAMPLE....WOW!!!this is string example....wow!!!

str = " this is string example....wow!!! ";print str.lstrip();str = "88888888this is string example....wow!!!8888888";print str.lstrip('8');#resultthis is string example....wow!!!this is string example....wow!!!8888888

?

str = "     this is string example....wow!!!     ";print str.rstrip();str = "88888888this is string example....wow!!!8888888";print str.rstrip('8');#result   this is string example....wow!!!88888888this is string example....wow!!!

?

?

?maketrans()看例子吧:例子中实际上是把对应的字母替换成数字。

from string import maketrans   # Required to call maketrans function.intab = "aeiou"outtab = "12345"trantab = maketrans(intab, outtab)str = "this is string example....wow!!!";print str.translate(trantab);#resultth3s 3s str3ng 2x1mpl2....w4w!!!

?

?

str = "this is really a string example....wow!!!";print "Max character: " + max(str);str = "this is a string example....wow!!!";print "Max character: " + max(str);#resultMax character: yMax character: x

?replace()用新字符替换旧字符

str = "this is string example....wow!!! this is really string";print str.replace("is", "was");          print str.replace("is", "was", 3);#resultthwas was string example....wow!!! thwas was really stringthwas was string example....wow!!! thwas is really string

?rfind()返回指定指定范围内,子串最后出现的索引,找不到返回-1。例如:

str = "this is really a string example....wow!!!";str1 = "is";print str.rfind(str1);print str.rfind(str1, 0, 10);print str.rfind(str1, 10, 0);print str.find(str1);print str.find(str1, 0, 10);print str.find(str1, 10, 0);#result55-122-1

?str = "this is string example....wow!!!";print str.rjust(50, '0');#result000000000000000000this is string example....wow!!!

?zfill()用“0”进行填充。看例子吧:

str = "this is string example....wow!!!";print str.zfill(40);print str.zfill(50);#result00000000this is string example....wow!!!000000000000000000this is string example....wow!!!

?

?

?

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";print str.split( );print str.split(' ', 1 );#result['Line1-abcdef', 'Line2-abc', 'Line4-abcd']['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

?str = "this is string example....wow!!!";print str.title();#resultThis Is String Example....Wow!

from string import maketrans # Required to call maketrans function.intab = "aeiou"outtab = "12345"trantab = maketrans(intab, outtab)str = "this is string example....wow!!!";print str.translate(trantab, 'xm');#resultth3s 3s str3ng 21pl2....w4w!!!

?

热点排行