str类型笔记

 1 # 居中 第二个参数空白处用*填充
 2 a1 = "alex"
 3 ret = a1.center(20,'*')
 4 print(ret)
 5 
 6 # 计算a出现了几次 第二个参数是计算范围
 7 a1 = "alex is alph"
 8 ret = a1.count("al")
 9 ret = a1.count('al',0,5)
10 print(ret)
11 
12 # 判断以什么字符结尾
13 temp = "hello"
14 # 获取大于等于0的位置,小余2的位置
15 print(temp.endswith('e',0,2))
16 
17 #转换table为空格
18 content = "hello\t999"
19 print(content)
20 
21 print(content.expandtabs())
22 
23 # 查找 如果找到了返回找到的位置,没找到返回-1
24 s = "alex  hello"
25 print(s.find("ex"))
26 
27 # 字符串格式化
28 s = "hello {0}, age {1}"
29 print(s)
30 # # {0} 是占位符
31 new1 = s.format("alex", 19)
32 print(new1)
33 
34 # 查找 如果找到了返回找到的位置,没找到报错
35 s = "alex  hello"
36 print(s.index("ex"))
37 
38 # 连接
39 li = ["alex","eric"]
40 # 元祖也可以
41 li = ("alex","eric")
42 ret = "_".join(li)
43 print(ret)
44 
45 #分割 前中后 三部分,以元组为单位
46 s = "alex SB eric"
47 print(s.partition('SB'))
48 
49 #替换
50 s = "alex SB alex"
51 print(s.replace("al","BB",1))
52 
53 # 从右往左找
54 s = "alex SB alex"
55 print(s.rfind("SB"))
56 
57 # 分割
58 s = "alexalex"
59 print(s.split("e"))
60 
61 #根据换行符进行分割
62 s = "alex\nalex"
63 print(s.split("\n"))
64 
65 #判断某个字符是否是某个字符串开始
66 s = "alexalex"
67 print(s.startswith("al"))
68 
69 # 大写变小写,小写变大写
70 s = "AlExalEX"
71 print(s.swapcase())
72 
73 # 把字符串变成标题
74 s = "AlExalEX"
75 print(s.title())
76 # 把字符串变成大写
77 s = "AlExalEX"
78 print(s.upper())

 

posted @ 2017-07-26 19:43  孤独的精彩  阅读(107)  评论(0编辑  收藏  举报