基本数据类型-str

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

猜你喜欢

转载自www.cnblogs.com/penphy/p/9261935.html
今日推荐