1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#字符串操作,字符串不能修改,可以查。字符串修改之后会生成另一个内存地址。
 
#格式
name  =  "abcdef\tabcdef"
 
#方法
print (name.capitalize())         #首字母大写
print (name.count( "a" ))           #统计字符串中a的数量
print (name.casefold())           #未知的
print (name.center( 20 , "-" ))       #打印20个字符,name变量里边的字符串不够,用-补上,字符串在居中位置
print (name.encode())             #转成二级制
print (name.endswith( "def" ))      #字符串是否以def结束,是返回True,否则False
print (name.expandtabs(tabsize = 30 )) #将\t转成多少个空格
print (name.find( "a" ))              #查找a在字符串里边的第一个下标
#name.format() 和name.format_map()
n1  =  "my name is {name},i am {year} old."
print (n1. format (name = "aaa" ,year = 20 ))
print (n1.format_map({ "name" : "aaa" , "year" : "20" }))
#
name  =  "abcdefabcdef"
print (name.index( "b" ))               #查找b在字符串里边的第一个下标
print (name.isalnum())                #是不是英文字母或数字的阿拉布数字
print (name.isalpha())                #是不是纯英文的
print (name.isdecimal())              #是否是十进制
print (name.isdigit())                #是否是一个整数
print (name.isidentifier())           #判断是不是一个合法的标识符,是不是一个合法的变量名
print (name.islower())                #判断字符串是否全是小写
print (name.isnumeric())              #是否是一个整数
print (name.isspace())                #是否是一个空格
print (name.istitle())                #是否是一个title,每个字符串首字母都大写
print (name.isprintable())            #是否能打印,不能打印的东西有驱动文件、终端设备,用途少;
print (name.isupper())                #判断字符串是否全是大写
 
#name.join()
print ( "+" .join([ "1" , "2" , "3" ]))      #循环["1","2","3"]列表,用+号把元素都组合起来
 
print (name.ljust( 20 , "-" ))            #打印20个字符,name变量的字符串不够20,字符串左对齐,用-在最后补齐
print (name.rjust( 20 , "-" ))            #打印20个字符,name变量的字符串不够20,字符串右对齐,用-在前面补齐
print (name.lower())                   #把字符串所有大写编程小写
print (name.upper())                   #把字符串所有小写编程大写
#name.lstrip()  name.rstrip()   name.strip()
print ( "\naaa\n" .lstrip())            #去掉字符串左边的回车
print ( "\naaa\n" .rstrip())            #去掉字符串右边的回车
print ( "     aaa  bbb" .strip())       #去掉字符串所有的开头和尾部的回车和空格
 
#name.maketrans()   用于以前密码表
name  =  str .maketrans( "abcedf" , "123456" ) #前边的字母和后边的数字 数量保持一样
print ( "acfghi" .translate(name))          #把acf在name里边的对应数字打印出来,在里面没有的打印本身
 
name  =  "abcdefabcdef"
print (name.partition( "cde" ))              #从左到右用字符串中间的cde将字符串分割成三个元素转换成元组类型。
#输出:('ab', 'cde', 'fabcdef')
 
print (name.replace( "a" , "B" ))              #将所有的a替换成B
print (name.replace( "a" , "B" , 1 ))            #将第一个a替换成B
print (name.rfind( "b" ))                    #从左往右查找字符b,找到最后边的b返回b的下标
print (name.rindex( "b" ))                   #从左往右查找字符b,找到最后边的b返回b的下标
print (name.rpartition( "cde" ))             #从右到左用字符串中间的cde将字符串分割成三个元素转换成元组类型。
#输出:('abcdefab', 'cde', 'f')
 
#name.split()   把字符串按照括号里的字符分开,转成列表
#例如:
print ( "1+2+3+4" .split( "+" ))              #输出:['1', '2', '3', '4']
print ( type ( "1+2+3+4" .split( "+" )))        #输出:<class 'list'>
 
#name.splitlines()  遇到字符串有换行的,用换行分开
#例如
print ( "1+2\n+3+4" .splitlines())          #输出['1+2', '+3+4']
 
name  =  "AbcdefaBcdef12"
print (name.swapcase())                    #将字符串里边的大写或者小写转换成相反的小写或者大写
print (name.startswith( "a" ))               #判断字符串是不是以a开头 是返回True 否则False
print (name.startswith( "A" ))               #判断字符串是不是以A开头 是返回True 否则False
 
name  =  "abc def"
print (name.title())                       #把字符串变成title,首字母大写
print (name.zfill( 20 ))                     #打印20个字符,name的字符串不够,用0从前补充。