字符串的方法使用

 1 name = "hello my 	 name is {name} and {age}  "
 2 print(name.capitalize())# 首字母大写
 3 print(name.count("a")) # 统计个数,可以指定位置
 4 print(name.encode())  # 编码 ,decode() 解码
 5 print(name.endswith("ex")) # 判断以什么结尾(邮件.com)
 6 print(name.find("name")) # 查找字符串所在位置
 7 print('alex lil'.rfind('l')) # 相同字符找到最右边的值返回
 8 print(name[name.find("name"):]) # 查找位置并切片
 9 print(name.format(name='Temu',age=23)) #字符串 变量赋值
10 print(name.format_map(  {'name':'Temu','age':12}  )) #用于字典
11 print('abC23'.isalnum())# 只识别0~9,a~z(包括大写)
12 print('abA'.isalpha())# 只识别a~z(包括大写)
13 print('1A'.isdecimal()) #只识别 十进制 (不常用)
14 print('1A'.isdigit()) #只识别整数   ( 等同 isnumeric() )
15 print('a 1A'.isidentifier()) #判读是不是一个合法的标识符
16 print('33ab'.islower()) # 只识别小写和数字
17 
18 print('python go'.replace('p','P',1))# 替换  1是限定次数
19 print('+'.join( ['1','2','3'])  )# 字符串间隔
20 
21 print('python temu'.title()) #字符串首字母 大写
22 print('My Name Is  '.istitle()) #判断每个字符串首字母是否大写 ->返回
23 print('My Name Is  '.isupper()) # 只识别大写和数字,特殊字符
24 
25 print( 'abc'.lower()  ) #小写转大写
26 print( 'abc'.upper()  )# 大写转小写
27 print('AbcDe'.swapcase()) # 大小写互相转换
28 print( '  helloword 
'.strip()  )#删除两边空格或回车
29 
30 mystr="hello python 我,你,他"
31 num=str.maketrans("op你","abc")   #o->a,p->b,你->c
32 print(mystr.translate(num))#翻译str.maketrans并替换
33 
34 print("逗比 is you".startswith("")) #判断开头
35 
36 print('1+2+3+4'.split('+')) #提取(除去+以外)
37 print('1+2
+3+4'.splitlines())# 提取,(除去换行) 识别不同系统换行符
38 
39 
40 print(max("abchsdxpew")) #编号最大的字符
41 print(min("abchsdxpew")) #编号最小的字符
42 
43  44 print(''.isspace())# 识别空格
45 print('My Name Is  '.isprintable()) #tty file ,drive file
46 print( name.ljust(50,'-')  )#居左
47 print( name.rjust(50,'-')  )#居右
即使明日天寒地冻路远马亡
原文地址:https://www.cnblogs.com/gamaboy/p/7376586.html