Python string_Detailed 3-end built-in function

The following method runs on python2 and uses python2 for encoding.

After comparing python3, we find that it is basically the same, that is, after print, add (), which can be run under python3 by using functions.

The method that works for unicode has been removed because it is temporarily unavailable and will be learned separately if needed in the future

Put forward the format method and write a separate article

If there are any mistakes, please correct them more by your predecessors. If a novice gets started, he will inevitably have his own narrowness and wrong thinking. But if you mislead others, it is true.Not so good, thank you

  1 #coding:utf-8
  2 
  3 s1="http"
  4 s2="http:/ /www.cnblogs.com/sub2020/p/7988111.html"
  5 #Get String Length
  6 print "len(s1):%d , len(s2):%d" %(len(s1),len(s2))
  7 
  8 #string.upper() Transformation string Lowercase letters in
  9 #string.lower() Transformation string All uppercase characters in.
 10 s3=s2.upper()
 11 print "s3=s2.upper() :%s" %s3
 12 print "s3.lower() :%s" %s3.lower()
 13 
 14 # string.capitalize() Capitalize the first character of a string
 15 print "s1.capitalize() :",s1.capitalize()
 16 
 17 # string.title()
 18 #Return"Titleization"Of string,That is, all words start with uppercase letters and the rest with lowercase letters(see istitle())
 19 print "s2.title() :%s" % s2.title()
 20 
 21 # string.istitle()
 22 #If string Is titled(see title())Then return True,Otherwise return False
 23 print "s2.title().istitle() :%s" % s2.title().istitle()
 24 print "s2.istitle() :%s" % s2.istitle()
 25 
 26 #string.swapcase() Flip string Uppercase and lowercase in
 27 #First put s2 Titled, as in the previous example, then flip case, output
 28 print "s2.title().swapcase() :%s" % s2.title().swapcase()
 29 
 30 # string.split(str="", num=string.count(str))
 31 #with str Slice for separator string,If num With a specified value, only separate num Substring
 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 #Return str stay string The number of times it appears, if beg perhaps end Specify returns the specified range str Number of occurrences
 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 #Testing str Is it included in string Medium, if beg and end Specifies the range, checks whether it is included in the specified range, returns the starting index value, otherwise returns-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 #Check if the string is obj At the beginning, yes goes back True,Otherwise return False. If beg and end Specify a value, then check within the specified range.
 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)) Be similar to find()Function, but look from the right.
 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 #with find()The same way, just if str Be not in string An exception will be reported in.
 62 #string.rindex( str, beg=0,end=len(string)) Be similar to index(),But from the right.
 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 #If string Returns at least one character and all characters are letters or numbers True,Otherwise return False
 69 print "s2.isalnum() :" ,s2.isalnum()
 70 print "list1[3].isalnum() :" ,list1[3].isalnum()
 71 
 72 # string.isalpha()
 73 #If string Returns at least one character and all characters are letters True,Otherwise return False
 74 print "s2.isalpha() :" ,s2.isalpha()
 75 print "list1[4].isalpha() :" ,list1[4].isalpha()
 76 
 77 # string.isdigit()
 78 #If string Only numbers are returned True Otherwise return False.
 79 print "s2.isdigit() :" ,s2.isdigit()
 80 print "list1[4].isdigit() :" ,list1[4].isdigit()
 81 
 82 # string.islower()
 83 #If string Contains at least one case-sensitive character, and all of these(Case sensitive)If all characters are lowercase, return True,Otherwise return False
 84 print "s2.islower() :" ,s2.islower()
 85 print "list1[4].islower() :" ,list1[4].islower()
 86 
 87 # string.isupper()
 88 #If string Contains at least one case-sensitive character, and all of these(Case sensitive)If all characters are uppercase, return True,Otherwise return False
 89 print "s3.isupper() :%s" % s3.isupper()
 90 print "list1[4].isupper() :%s" % list1[4].isupper()
 91 
 92 # string.isspace()
 93 #If string Contains only spaces, then returns True,Otherwise return False.
 94 print "s2.isspace() :" ,s2.isspace()
 95 print "list1[1].isspace() :" ,list1[1].isspace()
 96 
 97 # string.join(seq)
 98 #with string As a separator, the seq All elements in(The string representation of)Merge into a new string
 99 print "s1.join(list1) :%s" % s1.join(list1)
100 #Change to a more noticeable separator and the effect will be the same
101 s4="-*-"
102 print s4.join(list1)
103 
104 # max(str) Return string str The largest letter in the.
105 # min(str) Return string str The smallest letter in the.
106 print "max(s2) :%s" % max(s2)
107 print "min(s1) :%s" % min(s1)
108 
109 # string.partition(str)
110 #something like find()and split()Binding Complex,from str From the first place that appears,Put the string string A tuple divided into three elements 
111 #(string_pre_str,str,string_post_str),If string Does not contain str be string_pre_str == string.
112 #string.rpartition(str) Be similar to partition()function,But look from the right.
113 #The test results are displayed as follows: '/'As a separator, partition The method splits forward and backward, splitting the string into three tuples only, and cannot be replaced by formatting characters
114 print "partition s2 by '/' for 3 parts in a tuple:" ,s2.partition('/')
115 
116 # string.replace(str1, str2,  num=string.count(str1))
117 #hold string In str1 replace with str2,If num Specify, then the replacement does not exceed num second.
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 #Returns an original string centered,And fill the length with spaces width New string
123 print "s1.center(10,'*') :", s1.center(10,"*")
124 #When width<len When, return as is str
125 print "s1.center(2)     :", s1.center(2)
126 print "s1.center(5)     :", s1.center(5)
127 
128 # string.rstrip() delete string Character at the end of the string, defaulted to space.
129 print "s1.center(10,'*').rstrip('*') :%s" %s1.center(10,'*').rstrip('*')
130 # string.lstrip() Truncate string Character on the left, default to space.
131 print "s1.center(10,'*').lstrip('*') :%s" % s1.center(10,'*').lstrip('*')
132 # string.strip([obj]) stay string Upper Execution lstrip()and rstrip()
133 print "s1.center(10,'*').strip('*')  :%s" % s1.center(10,'*').strip('*')
134 
135 # string.zfill(width)
136 #Return length is width String, original string string Right aligned with 0 in front
137 print "s1.zfill(20) :%s" % s1.zfill(20)
138 #and center The same way, if width<len,Return the original string
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***

Keywords: Python encoding REST

Added by RockinPurdy on Mon, 29 Jun 2020 20:00:56 +0300