首页 > 技术文章 > str 操作方法

penphy 2018-11-27 20:36 原文

  1 # str 类,字符串
  2 # name ='alex'
  3 
  4 
  5 # 首字母变大写
  6 # test ='alex'
  7 # v= test.capitalize()
  8 # print(v)
  9 #
 10 # 大写全部变小写
 11 # test ='aLExALEX'
 12 # v= test.lower()
 13 # print(v)
 14 #         功能与lower的效果一样。但如果是他国的语言casefold比lower效果更好
 15 # test ='alexALEX'
 16 # v= test.casefold()
 17 # print(v)
 18 
 19 
 20 # 大小写转换: 大写变小写 小写变大写
 21 # test ='alexXXFFFEe'
 22 # v = test.swapcase()
 23 # print(v)
 24 #
 25 #
 26 # 从开始往后找,找到第一个之后,获取其未知
 27 # > 或 >=
 28 # test ='penphypenphy'
 29 # v = test.find('e',5,8)
 30 # print(v)
 31 #  输出7
 32 #
 33 #
 34 # 格式化,将一个字符串
 35 # test ='i am {name},age {a}'
 36 # print(test)
 37 # v = test.format(name='penphy',a='19')
 38 # print(v)
 39 #
 40 # 下面的例子得 count是计算p在 penphy字符串中出现的次数,从多少至多少
 41 # test = 'penphy'
 42 # v = test.count('p',0,100)
 43 # print(v)
 44 
 45 # 设置宽度并将内容居中
 46 # 20 代指总长度
 47 # 空白未知填充,一个字符,可有可无
 48 # test ='penphy'
 49 # v = test.center(20,'*')
 50 # print(v)
 51 # 输出:*******penphy*******
 52 
 53 # ljust是把字放左边
 54 # test ='alex'
 55 # v =test.ljust(20,'*' )
 56 # print(v)
 57 # 输出:alex****************
 58 
 59 # rjust是把字放右边
 60 # v1 =test.rjust(20,'*')
 61 # print(v1)
 62 # 输出:****************alex
 63 
 64 # 只能用0来填充,不怎么使用
 65 # test ='penhy'
 66 # v = test.zfill(8)
 67 # print(v)
 68 # 输出:000penhy
 69 
 70 
 71 # expandtabs,断句20.比如tab会补齐username缺的空格,以此类推
 72 # test = 'username\temail\tpassword\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123'
 73 # v= test.expandtabs(20)
 74 # print(v)
 75 #
 76 #
 77 # 检测字符串中是否只包含 字母和数字
 78 # test = 'abc123'
 79 # v = test.isalnum()
 80 # print(v)
 81 # 检测字符串中是否只由字母组成
 82 # test = 'penophy'
 83 # v = test.isalpha()
 84 # print(v)
 85 #
 86 # 判定字符串里输入的是否是数字
 87 # test ='二'
 88 # v1 = test.isdecimal()  十进制的小数,但不支持中文表示的数字
 89 # v2 =test.isdigit()   支持特殊的符号,但不支持中文表示的数字
 90 # v3 =test.isnumeric() 全都支持
 91 # print(v1,v2,v3)
 92 
 93 # 字母,数字,下划线 :数字不能开头 只要符合这个条件的 就是 '标识符'  def class符合前面的规则 比如:
 94 # a ='123'
 95 # v =a.isidentifier()
 96 # print(v)
 97 #
 98 # import keyword
 99 # print(keyword.iskeyword('def'))
100 # test ='def'
101 # v =test.isidentifier()
102 # print(v)
103 
104 # 是否存在不可显示的字符
105 # \t 制表符
106 # \n 换行
107 # test ='asdasdsad\n'
108 # v =test.isprintable()
109 # print(v)
110 
111 
112 # 判断是否全部是空格
113 # test ='  '
114 # v =test.isspace()
115 # print(v)
116 
117 # 判断是否是标题,首字母需大写
118 # test = 'Return True if all characters in S are alphabetic'
119 # v1 = test.istitle()
120 # print(v1)               False
121 # v2 = test.title()
122 # print(v2)               Return True If All Characters In S Are Alphabetic
123 # v3 = v2.istitle()
124 # print(v3)               True
125 # 输出:                             ↑
126 
127 # 将字符串中的每一个元素按照指定分隔符进行拼接-----重要
128 # test = '你是风儿我是沙'
129 # print(test)
130 # t =' '
131 # v = ' '.join(test)
132 # print(v)
133 
134 # 判断是否全部为大小写 和 转换为大小写
135 # test ='Alex'
136 # v1 =test.islower()
137 # v2 =test.lower()
138 # print(v1,v2)
139 # 输出:False  alex
140 # v1 =test.isupper()
141 # v2 =test.upper()
142 # print(v1,v2)
143 # 输出:False ALEX
144 
145 
146 
147 # 去除左边空白
148 # test ='alex'
149 # v =test.lstrip()
150 # print(v)
151 # 去除右边的空白
152 # v =test.rstrip()
153 # print(v)
154 # 去除两边的空白
155 # v=test.strip()
156 # print(v)
157 
158 # test ='\nalex'
159 # print(test)
160 # 去除\t,\n
161 # v =test.lstrip()
162 # print(v)
163 
164 
165 
166  # 移除指定字符串
167  # 有限匹配↓
168  # 去除左边的指定字符
169 # test ='xalex'
170 # v= test.lstrip('xa')
171 # print(v)
172 # 输出:lex
173 #  去除右边的指定字符 先进行最多匹配
174 # v= test.rstrip('9lexxex')
175 # print(v)
176 # 输出:xa
177 #  去除两边的指定字符
178 # v1 =test.strip('x')
179 # print(v1)
180 # 输出:ale
181 
182 
183 # v ="eawesadasdasgffdgsergerherg.ergerg"
184 # m =str.maketrans('aeiou','12345')
185 # new_v =v.translate(m)
186 # print(new_v)
187 # 输出:21w2s1d1sd1sgffdgs2rg2rh2rg.2rg2rg
188 
189 
190 
191 #  做分割:↓
192 # 用法区别:partition 包含分割的元素 split不包含分割的元
193 # test ='dsfsdfsdrewrdddfsdsfds'
194 # v =test.partition('s')
195 # print(v)
196 # 输出:('d', 's', 'fsdfsdrewrdddfsdsfds')
197 # v =test.rpartition('sf')
198 # print(v)
199 # 输出:('dsfsdfsdrewrdddfsd', 'sf', 'ds')
200 # split的弊端是不能提取 使字符串分割的值 比如下面的's'
201 # test ='qwesxcxzsadsadasdasf'
202 # v = test.split('s')
203 # print(v)
204 # 输出:['qwe', 'xcxz', 'ad', 'ada', 'da', 'f']
205 # v = test.split('s',3)
206 # print(v)
207 # 输出:['qwe', 'xcxz', 'ad', 'adasdasf']
208 
209 # 分割,只能根据True,False:是否保留换行
210 # test ='sadasdaewq\nasdsasldksal\n'
211 # v = test.splitlines(True)
212 # print(v)
213 
214 # 替换:
215 # test ='alexalexalex'
216 # v = test.replace('ex','bbb')
217 # print(v)
218 # 输出:albbbalbbbalbbb
219 # v =test. replace('ex','bbb',2)
220 # print(v)                    ↓
221 # 输出:albbbalbbbalex         替换前2个字符
222 
223 
224 
225 
226 # 以xxxx开头,以xx结尾
227 # test = 'backhand1.1.1'
228 # v =test.startswith('ba')
229 # print(v)
230 # v =test.endswith('1')
231 # print(v)
232 
233 
235 # 字符串一旦创建就不可修改
236 # 一旦修改或者拼接,都会造成重新生成字符串
237 # name ='sunpengfei'
238 # age = '18'
239 # info = name + age
240 # print(info)
241 
242 
243 # 索引,下标,获取字符串中的某一个字符
244 # test ='alexjk'
245 # v = test[3]
246 # print(v)
247 # 输出:x
248 
249 # 切片
250 # v = test[0:-1]  #0=<   <-1
251 # print(v)
252 # 输出alexj
253 
254 # len获取当前字符串中由多少个字符组成
255 # test ='alexad'
256 # v =len(test)
257 # print(v)
258 # 输出:6
259 
260 # 注意:
261 # v =len('asdasd')
262 # v ='_'.join('dasdasdas')
263 # print(v)
264 
265 # li = [11,22,33,44,55,'sad']
266 # len('sadsadasdasd')
267 # len(li)
268 
269 # test ='妹子有本事你到我床上来'
270 #  index = 0
271 # while index< len(test):
272 #     v = test[index]
273 #     print(v)
274 #     index +=1
275 # print('=====================')
276 # for循环:
277 
278 # for 变量名 in 字符:串
279 # for i in test:
280 #print(i)
281 
282 
283 # test ='妹子有本事你到我床上来'
284 # for item in test:
285 #     print(item)
286 #     break
287 
288 # for item in test:
289 #     continue
290 #     print(item)
291 
292 # range:帮助创建连续的数字,通过设置步长来指定不连续
293 # v = range(0,100,5)
294 # # print(v)
295 # for item in v:
296 #     print(item)
297 
298 # test =input('>>>')
299 # print(test)
300 # l =len(test)
301 # print(l)
302 #
303 # r = range(0,l)
304 # for item in r:
305 #     print(item, test[item])
306 # 总结上面的:
307 # test = input('>>>')
308 # for item in range(0,len(test)):
309     # print(item,test[item])
310 
311 s = '  '
312 
313 while True:
314     v1 = input('>>>')
315     v2 = input('>>>')
316     v3 = input('>>>')
317     template = '{0}\t{1}\t{2}\n'
318     v = template.format(v1, v2, v3)
319     s = s + v
320     break
321 print(s.expandtabs(20))

 

推荐阅读