Python字符串常见操作

字符串常见操作:

  1 #首字母大写
  2 str = "my name is zhangsan"
  3 print(str.capitalize())
  4 >>My name is zhangsan
  5 
  6 #统计字符串里有几个字符
  7 str = 'my name is zhangsan'
  8 print(str.count('i')) #print(str.count('a',5,18))
  9 >>1
 10 
 11 #居中,不足按参数形式补全
 12 str = 'my name is zhangsan'
 13 print(str.center(50,'-'))
 14 >>---------------my name is zhangsan----------------
 15 
 16 #左对齐,右边不足按参数形式补全
 17 str = "my name is zhangsan, and age is 22"
 18 print (str.ljust(50,'-'))
 19 >>my name is zhangsan, and age is 22----------------
 20 
 21 #右对齐,左边不足按参数形式补全
 22 str = "my name is zhangsan, and age is 22"
 23 print (str.rjust(50,'-'))
 24 >>----------------my name is zhangsan, and age is 22
 25 
 26 #制表符转换为空格,参数为空格数量
 27 str = 'my name is zh\tangsan'
 28 print(str)
 29 print(str.expandtabs(10))
 30 
 31 #判断是否为相应字符结尾
 32 str = 'my name is zhangsan'
 33 print(str.endswith('san'))
 34 
 35 #string转换为bytes
 36 str = '我爱中国'
 37 print(str.encode())
 38 >>b'\xe6\x88\x91\xe7\x88\xb1\xe4\xb8\xad\xe5\x9b\xbd'
 39 
 40 #bytes转换为string
 41 bs = b'\xe6\x88\x91\xe7\x88\xb1\xe4\xb8\xad\xe5\x9b\xbd'
 42 print(bs.decode())
 43 >>我爱中国
 44 
 45 #在规定位置查找字符,找到返回index,找不到-1
 46 str = 'my name is zhangsan'
 47 print(str.find('a',3,10))
 48 
 49 #格式化字符串
 50 str = 'my {} is {}'
 51 print(str.format('name','zhangsan'))
 52 >>my name is zhangsan
 53 #可以不按顺序
 54 str = 'my {1} is {0}'
 55 print(str.format('name','zhangsan'))
 56 >>my zhangsan is name
 57 #也可以这样
 58 str = 'my {a} is {b}'
 59 print(str.format(b='name',a='zhangsan!!!'))
 60 >>my zhangsan!!! is name
 61 
 62 #用map方式格式化
 63 str = "my name is {name}, and age is {age}"
 64 print(str.format_map({'name':'zhangsan','age':'22'}))
 65 >>my name is zhangsan, and age is 22
 66 
 67 #判断是否全大写
 68 str = 'AAA'
 69 print(str.isupper())
 70 >>True
 71 
 72 #判断首字母大写
 73 str = 'Aa'
 74 print(str.istitle())
 75 >>True
 76 
 77 #判断是不是空格
 78 str = ' '
 79 print(str.isspace())
 80 >>True
 81 
 82 #判断是不是由数字字符组成的字符串
 83 str = '111'
 84 print(str.isnumeric())
 85 >>True
 86 
 87 #判断是否全字母组成
 88 str = 'aaaa'
 89 print(str.isalpha())
 90 >>True
 91 
 92 #检测指定str是否存在与原str中,可以指定开始结束,与find()区别在于,如果没查到会报异常
 93 str = "my name is zhangsan, and age is 22"
 94 print(str.index('a'))
 95 >>4
 96 print(str.index('x'))
 97 >>ValueError: substring not found
 98 
 99 #判断是否小写字母组成
100 str = 'aaa'
101 print(str.islower())
102 >>True
103 
104 #判断是否字母和数字组成
105 str = 'a12Aaa'
106 print(str.isalnum())
107 >>True
108 
109 #判断是否只由数字构成
110 str = '111'
111 print(str.isdigit())
112 >>True
113 
114 #用于将序列中的元素以指定的字符连接生成一个新的字符串
115 str = "-";
116 seq = ("a", "b", "c"); # 字符串序列
117 print str.join( seq );
118 >>a-b-c
119 
120 #转换字符串中所有大写字符为小写。
121 str = "my nAme is zhANGSAN, and age is 22"
122 print (str.lower())
123 >>my name is zhangsan, and age is 22
124 
125 #移除字符串头尾指定的字符(默认为空格)
126 str = "*****this is string example....wow!!!*****"
127 print (str.strip( '*' ))
128 >>this is string example....wow!!!
129 
130 #截掉字符串左边的空格或指定字符,默认为空格
131 str = "1111my name is zhangsan, and age is 221111"
132 print (str.lstrip('1'))
133 >>my name is zhangsan, and age is 221111
134 
135 #截掉字符串右边的空格或指定字符,默认为空格
136 str = "1111my name is zhangsan, and age is 221111"
137 print (str.rstrip('1'))
138 >>1111my name is zhangsan, and age is 22
139 
140 #intab替换为outtab,一一对应,转换为ASCII码格式,translate()把ASCII码转换为对于字符
141 intab = "aeiou"
142 outtab = "12345"
143 trantab = str.maketrans(intab, outtab)
144 print(trantab)
145 str = "this is string example....wow!!!"
146 print (str.translate(trantab))
147 >>{97: 49, 101: 50, 105: 51, 111: 52, 117: 53}
148 >>th3s 3s str3ng 2x1mpl2....w4w!!!
149 
150 #返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
151 str = "http://www.w3cschool.cc/"
152 print(str.partition('//'))
153 >>('http:', '//', 'www.w3cschool.cc/')
154 
155 #返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间
156 str = "1111my name is zhangsan, and age is 221111"
157 print (str.rindex('isx'))
158 >>ValueError: substring not found
159 
160 #返回字符串最后一次出现的位置,如果没有匹配项则返回-1
161 str = "1111my name is zhangsan, and age is 221111"
162 print (str.rfind('isx'))
163 >>-1
164 
165 #方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次
166 str = "this is string example....wow!!! this is really string"
167 print (str.replace("is", "was"))
168 print (str.replace("is", "was", 3))
169 >>thwas was string example....wow!!! thwas was really string
170 >>thwas was string example....wow!!! thwas is really string
171 
172 #把字符串切割成列表,默认空格为切割符
173 str = "       1111my name is zhangsan, and age is 221111   "
174 print (str.split())
175 >>['1111my', 'name', 'is', 'zhangsan,', 'and', 'age', 'is', '221111']
176 
177 #按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
178 str1 = 'ab c\n\nde fg\rkl\r\n'
179 print(str1.splitlines())
180 str2 = 'ab c\n\nde fg\rkl\r\n'
181 print(str2.splitlines(True))
182 
183 #用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
184 str = "this is string example....wow!!!"
185 print (str.startswith( 'this' ))
186 print (str.startswith( 'string', 8 ))
187 print (str.startswith( 'this', 2, 4 ))
188 >>True
189 >>True
190 >>False
191 
192 #转换成大写
193 str = "this is string example....wow!!!"
194 print (str.swapcase())
195 >>THIS IS STRING EXAMPLE....WOW!!!
196 
197 #将字符串中的小写字母转为大写字母。
198 str = "this is string example....wow!!!"
199 print (str.upper())
200 >>THIS IS STRING EXAMPLE....WOW!!!
201 
202 #首字母大写
203 str = "this is string example....wow!!!"
204 print (str.title())
205 >>This Is String Example....Wow!!!
206 
207 #左边补零
208 str = "123456789"
209 print (str.zfill(9))
210 print (str.zfill(10))
211 print (str.zfill(11))
212 >>123456789
213 >>0123456789
214 >>00123456789
posted @ 2019-01-12 14:02  zhangshh  阅读(244)  评论(0编辑  收藏  举报