python string_3 end 内建函数详解

以下方法,是在python2上运行的,编码也使用的是python2,

在对比python3后,发现,基本相同,也就是说在print后补上(),使用函数方式,是可以在python3下运行的,

删除了针对unicode起作用的方法,因为暂时用不上,以后如果需要,将会单独学习

将format方法提出,将单独写一篇

如果有错误,请前辈多多指正,新手入门,难免有自己的狭隘与思维错误,但如果误导了他人,就真是。。。不太好了,谢谢

  1 #coding:utf-8
  2 
  3 s1="http"
  4 s2="http:/ /www.cnblogs.com/sub2020/p/7988111.html"
  5 #取得字符串长度
  6 print "len(s1):%d , len(s2):%d" %(len(s1),len(s2))
  7 
  8 #string.upper() 转换 string 中的小写字母为大写
  9 #string.lower() 转换 string 中所有大写字符为小写.
 10 s3=s2.upper()
 11 print "s3=s2.upper() :%s" %s3
 12 print "s3.lower() :%s" %s3.lower()
 13 
 14 # string.capitalize() 把字符串的第一个字符大写
 15 print "s1.capitalize() :",s1.capitalize()
 16 
 17 # string.title()
 18 #返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
 19 print "s2.title() :%s" % s2.title()
 20 
 21 # string.istitle()
 22 #如果 string 是标题化的(见 title())则返回 True,否则返回 False
 23 print "s2.title().istitle() :%s" % s2.title().istitle()
 24 print "s2.istitle() :%s" % s2.istitle()
 25 
 26 #string.swapcase() 翻转 string 中的大小写
 27 #先把s2标题化,与上例相同,然后翻转大小写,输出
 28 print "s2.title().swapcase() :%s" % s2.title().swapcase()
 29 
 30 # string.split(str="", num=string.count(str))
 31 #以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
 32 list1 = s2.split("/")
 33 print list1
 34 for x,y in enumerate(list1):
 35     print "list[%d]:%s" %(x,y)
 36 
 37 # string.count(str, beg=0, end=len(string)) 
 38 #返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
 39 print "s2.count('c') :%d" %s2.count('c')
 40 print "s2.count('c',15) :%d" %s2.count('c',15)
 41 print "s2.count('p',10,30) :%d" %s2.count('p',10,30)
 42 
 43 # string.find(str, beg=0, end=len(string))
 44 #检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
 45 print "s2.find('c') :%d" %s2.find('c')
 46 print "s2.find('c',15) :%d" %s2.find('c',15)
 47 print "s2.find('p',10,30) :%d" %s2.find('p',10,30)
 48 
 49 # string.startswith(obj, beg=0,end=len(string))
 50 #检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.
 51 print "s2.startswith('h') :%s" % s2.startswith('h')
 52 print "s2.startswith('c',15) :%s" % s2.startswith('c',15)
 53 print "s2.startswith('p',10,30) :" , s2.startswith('p',10,30)
 54 
 55 # string.rfind(str, beg=0,end=len(string)) 类似于 find()函数,不过是从右边开始查找.
 56 print "s2.rfind('c') :%d" %s2.rfind('c')
 57 print "s2.rfind('c',15) :%d" %s2.rfind('c',15)
 58 print "s2.rfind('p',10,30) :%d" %s2.rfind('p',10,30)
 59 
 60 # string.index(str, beg=0, end=len(string))
 61 #跟find()方法一样,只不过如果str不在 string中会报一个异常.
 62 #string.rindex( str, beg=0,end=len(string)) 类似于 index(),不过是从右边开始.
 63 print "s2.index('c') :%d" %s2.index('c')
 64 print "s2.index('c',15) :%d" %s2.index('c',15)
 65 #print "s2.index('p',10,30) :%d" %s2.index('p',10,30)
 66 
 67 # string.isalnum()
 68 #如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
 69 print "s2.isalnum() :" ,s2.isalnum()
 70 print "list1[3].isalnum() :" ,list1[3].isalnum()
 71 
 72 # string.isalpha()
 73 #如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
 74 print "s2.isalpha() :" ,s2.isalpha()
 75 print "list1[4].isalpha() :" ,list1[4].isalpha()
 76 
 77 # string.isdigit()
 78 #如果 string 只包含数字则返回 True 否则返回 False.
 79 print "s2.isdigit() :" ,s2.isdigit()
 80 print "list1[4].isdigit() :" ,list1[4].isdigit()
 81 
 82 # string.islower()
 83 #如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
 84 print "s2.islower() :" ,s2.islower()
 85 print "list1[4].islower() :" ,list1[4].islower()
 86 
 87 # string.isupper()
 88 #如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
 89 print "s3.isupper() :%s" % s3.isupper()
 90 print "list1[4].isupper() :%s" % list1[4].isupper()
 91 
 92 # string.isspace()
 93 #如果 string 中只包含空格,则返回 True,否则返回 False.
 94 print "s2.isspace() :" ,s2.isspace()
 95 print "list1[1].isspace() :" ,list1[1].isspace()
 96 
 97 # string.join(seq)
 98 #以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
 99 print "s1.join(list1) :%s" % s1.join(list1)
100 #换一种更明显的分隔符,效果一样
101 s4="-*-"
102 print s4.join(list1)
103 
104 # max(str) 返回字符串 str 中最大的字母。
105 # min(str) 返回字符串 str 中最小的字母。
106 print "max(s2) :%s" % max(s2)
107 print "min(s1) :%s" % min(s1)
108 
109 # string.partition(str)
110 #有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 
111 #(string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.
112 #string.rpartition(str) 类似于 partition()函数,不过是从右边开始查找.
113 #测试结果显示以 '/'为分隔符,partition方法从前向后分割,只会将字符串分割为3份的 元组,且无法用格式化字符代替
114 print "partition s2 by '/' for 3 parts in a tuple:" ,s2.partition('/')
115 
116 # string.replace(str1, str2,  num=string.count(str1))
117 #把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.
118 print "use s4 replace '/' in s2 :%s" % s2.replace('/',s4)
119 print "use s4 replace '/' in s2 for 2 times :%s" % s2.replace('/',s4,2)
120 
121 # string.center(width) 
122 #返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
123 print "s1.center(10,'*') :", s1.center(10,"*")
124 #当width<len时,原样返回str
125 print "s1.center(2)     :", s1.center(2)
126 print "s1.center(5)     :", s1.center(5)
127 
128 # string.rstrip() 删除 string 字符串末尾的字符,默认为空格.
129 print "s1.center(10,'*').rstrip('*') :%s" %s1.center(10,'*').rstrip('*')
130 # string.lstrip() 截掉 string 左边的字符,默认为空格.
131 print "s1.center(10,'*').lstrip('*') :%s" % s1.center(10,'*').lstrip('*')
132 # string.strip([obj]) 在 string 上执行 lstrip()和 rstrip()
133 print "s1.center(10,'*').strip('*')  :%s" % s1.center(10,'*').strip('*')
134 
135 # string.zfill(width)
136 #返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0
137 print "s1.zfill(20) :%s" % s1.zfill(20)
138 #与center方法一样,如果width<len,返回原字符串
139 print "s2.zfill(20) :%s" % s2.zfill(20)

 output

len(s1):4 , len(s2):46
s3=s2.upper() :HTTP:/ /WWW.CNBLOGS.COM/SUB2020/P/7988111.HTML
s3.lower() :http:/ /www.cnblogs.com/sub2020/p/7988111.html
s1.capitalize() : Http
s2.title() :Http:/ /Www.Cnblogs.Com/Sub2020/P/7988111.Html
s2.title().istitle() :True
s2.istitle() :False
s2.title().swapcase() :hTTP:/ /wWW.cNBLOGS.cOM/sUB2020/p/7988111.hTML
['http:', ' ', 'www.cnblogs.com', 'sub2020', 'p', '7988111.html']
list[0]:http:
list[1]: 
list[2]:www.cnblogs.com
list[3]:sub2020
list[4]:p
list[5]:7988111.html
s2.count('c') :2
s2.count('c',15) :1
s2.count('p',10,30) :0
s2.find('c') :12
s2.find('c',15) :20
s2.find('p',10,30) :-1
s2.startswith('h') :True
s2.startswith('c',15) :False
s2.startswith('p',10,30) : False
s2.rfind('c') :20
s2.rfind('c',15) :20
s2.rfind('p',10,30) :-1
s2.index('c') :12
s2.index('c',15) :20
s2.isalnum() : False
list1[3].isalnum() : True
s2.isalpha() : False
list1[4].isalpha() : True
s2.isdigit() : False
list1[4].isdigit() : False
s2.islower() : True
list1[4].islower() : True
s3.isupper() :True
list1[4].isupper() :False
s2.isspace() : False
list1[1].isspace() : True
s1.join(list1) :http:http httpwww.cnblogs.comhttpsub2020httpphttp7988111.html
http:-*- -*-www.cnblogs.com-*-sub2020-*-p-*-7988111.html
max(s2) :w
min(s1) :h
partition s2 by '/' for 3 parts in a tuple: ('http:', '/', ' /www.cnblogs.com/sub2020/p/7988111.html')
use s4 replace '/' in s2 :http:-*- -*-www.cnblogs.com-*-sub2020-*-p-*-7988111.html
use s4 replace '/' in s2 for 2 times :http:-*- -*-www.cnblogs.com/sub2020/p/7988111.html
s1.center(10,'*') : ***http***
s1.center(2)     : http
s1.center(5)     :  http
s1.center(10,'*').rstrip('*') :***http
s1.center(10,'*').lstrip('*') :http***
s1.center(10,'*').strip('*')  :http
0000000000000000http
http:/ /www.cnblogs.com/sub2020/p/7988111.html

***Repl Closed***
原文地址:https://www.cnblogs.com/sub2020/p/8017330.html