字符串方法

 1 name="my name is good asbxs"
 2 print(name.capitalize())# 首字母大写
 3 print("My NAME IS GOOD".casefold())#变小写
 4 print(name.center(20,"_"))#以"_"头尾相加补全字符串长度
 5 print (name.count("o",1,2))#查找字符串在整个字符串中的数量
 6 print("中文".encode("utf-8"))#将unicode编码转换成其他编码的字符串
 7 print(name.endswith("ood"))#判断是否以ood为结尾
 8 print("a	xbc","a	xbc".expandtabs(10))#增加tab长度
 9 print(name.find("m"))#查找字符索引
10 print(name[name.find("name"):])#字符串切片
11 print("My name is {name}".format(name="xk"))# 格式化替代{}}里面的标识
12 print("My name is {name}".format_map({"name":"jack"}))#以字典的形式格式化替代{}里面的标识
13 print(name.index("good"))#在字符串中索引
14 print("2","adasf-".isalnum())#包含英文数字
15 print("sisdf.".isalpha())#判断是否纯字母
16 print("102".isdecimal())#判断是否十进制
17 print(name.isdigit())#是否整数
18 print("----",name.isidentifier())#是否一个合法标识符
19 print("abcK".islower())#小写
20 print(name.isnumeric())#判断数字
21 print(name.isprintable())#tty file, drive file 判断是可否打印
22 print("   ".isspace())#空格
23 print("My Name Is jack".istitle())#判断首字母是否大写
24 print("ABC 中文".isupper())#判断字母是否全是大写
25 print(".".join(["","世界","fdg"]))#组合字符串
26 print(name.ljust(20,"a"))#.center 类似
27 print(name.rjust(20,"a"))#.center 类似
28 print(name.lower())#大写变小写
29 print("    abs  ".lstrip())#取消左边字符空格
30 print("    abs  ".rstrip())#取消右边字符空格
31 print("    abs  ".strip())#取消头尾字符空格
32 p=name.maketrans("abscds",'123456')
33 print("jackds".translate(p))# maketrans 和 translate 配合使用
34 print("jackestiankotng".partition("tt"))#区分字符串
35 print(name.replace('a','c'))
36 print(name)
37 print(name.split("s"))#分隔符
38 print('a
b
c'.splitlines())
39 print(name.startswith("m"))#endswith 类似 判断是否以参数开头
40 print('Jack'.swapcase())#大写转小写,小写转大写
41 # print('jack'.translate("123","125") )
42 print(name.zfill(50))#在左边用0填充一个数字字符串s,以填充字段。指定宽度的。字符串s从不被截断
原文地址:https://www.cnblogs.com/JIM-FAN/p/8484259.html