python之字符串处理

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: strformat.py
 5 #Author: frank
 6 #Email: frank0903@aliyun.com
 7 #Created Time:2018-04-28 12:23:00
 8 ############################
 9 
10 #槽内部对格式化的配置方式
11 #{<参数序号>:<格式控制标记>}
12 
13 #:           <填充>      <对齐>      <宽度>      <,>             <. 精度>               <类型>
14 #引导符号    用于填充的  < 左对齐    槽设定的     数字的          浮点数小数的精度          整数类型
15 #           单个字符   > 右对齐    输出宽度    千位分隔符      或 字符串最大输出长度       b,c,d,o,x,X
16 #                     ^ 居中对齐                                                     浮点数类型
17 #                                                                                    e,E,f,%
18 
19 a="{}:计算机{}的CPU占用率为{}%".format("2018-10-10", "C", 10)
20 print(a)
21 
22 #字符串处理函数
23 
24 #str.lower()
25 print("AbCdEfGh".lower())
26 #str.upper()
27 print("AbCdEfGh".upper())
28 
29 #str.split(sep=None) 返回一个列表,由str根据sep被分隔的部分组成
30 print("A,B,C".split(","))
31 
32 #str.count(sub) 返回子串sub在str中出现的次数
33 print("an apple a day".count("a"))
34 
35 #str.replace(old, new) 返回字符串str副本,所有old子串被替换为new
36 print("python".replace("n", "n123.io"))
37 
38 #str.center(width, [fillchar]) 字符串str根据宽度width居中,filechar可选.
39 print("python".center(20, "*"))
40 
41 #str.strip(chars) 从str中去掉在其左侧和右侧chars中列出的字符
42 print("= python= ".strip(" =np"))
43 
44 #str.join(ite) 在iter变量 除最后元素外每个元素后增加一个str
45 print(",".join("ABCDE"))

 

原文地址:https://www.cnblogs.com/black-mamba/p/8966995.html