1 # #def_ isspace判断是否全部是空格 2 # test="asldkf akdjfl;ksda" 3 # v=test.isspace() 4 # print(v) 5 6 #" istitle判断单词的第一个字母是否为大小写。 7 # test="Process finished with exit code 0" 8 # v=test.istitle() 9 # print(v) 10 # title把单词的第一个字母,全部转换成大写。 11 # test="Process finished with exit code 0" 12 # v=test.title() 13 # print(v) 14 15 # ****** join 16 # 将字符串中的每一个元素,按照指定分隔符进行拼接。 17 # test="你是风儿我是沙" 18 # print(test) 19 # t="58/" 20 # v=t.join(test) 21 # # t="*/"加上 v = t.join(test),可以换成这个公式,v="*/".join(test)结果是一样的。 22 # print(v) 23 24 #你*/是*/风*/儿*/我*/是*/沙 25 26 # ljutst()表示在字符串的右边加上 27 # test="dutianmu" 28 # v=test.ljust(20,"2") 29 # print(v) 30 31 #输出结果是:dutianmu222222222222 32 33 # 在字符串的前面加上字符。“r”代表左边 34 # test="ksdfeinh" 35 # v=test.rjust(20,"*") 36 # print(v) 37 # 输出结果:************ksdfeinh 38 39 # 20 代表总长度。 40 # * 空白位置填充,一个字符,可有可无 41 # test="dkeina" 42 # v=test.center(20,"中") 43 # print(v) 44 # 输出结果:中中中中中中中dkeina中中中中中中中 45 46 # zfill() 默认填充“0”,一般用不到 47 # test="laddek" 48 # v=test.zfill(20) 49 # print(v) 50 # #输出结果:00000000000000laddek 51 52 # islower判断是否全部为小写,lower是变成小写。 53 # test="Alex" 54 # v1=test.islower() 55 # v2=test.lower() 56 # print(v1,v2) 57 #输出结果:False alex 58 59 # isupper判断是否全部为大写;upper是把字符全变成大写 60 # test="Alex" 61 # v1=test.isupper() 62 # v2=test.upper() 63 # print(v1,v2) 64 #输出结果:False ALEX 65 66 #test=" alex " 67 #v=test.lstrip() #去除左边空白alex,包括空字符\t或者\n换行 68 #v=test.rstrip() #去除右边空白 alex 69 #v=test.strip() #去除左右空白 70 71 # 括号内加上字符串,就可以删除左/右边的相应的字母( 72 # test="x你好alexg" 73 # v=test.lstrip("x") #输出结果是:你好alexg 74 #v=test.rstrip("xg") #输出结果是:x你好ale 75 # test="xalexgdeea" #这个挺有意思的,值得研究一下。 76 # v=test.lstrip("daxaeadedxddfasdfzx") 77 # #v=test.rstrip("xg") 78 # print(v) 79 # txt = ",,,,,ssaaww.....banana" #删除所有前导字符(空格是要删除的默认前导字符) 80 # x = txt.lstrip(",.bnwasw") 81 # print(x) #结果是什么都没有 82 83 #test="alxe" 84 #v=test.isprintable() #判断是否可以打印,条件是不能有\t\n等不可见的 85 #print(v) #结果是False或者True 86 87 # test="axdel" 88 # test1="1325*" #用来替换用的,必须同被替换的一样长 89 # v="alskdjf;lkjsd;lfkiena" 90 # m=str.maketrans(test,test1) # maketrans与translate配合用,主要是用来替换的。 91 # new_v=v.translate(m) 92 # print(new_v) 93 94 test="kjdflkajdsfiejk" #分割作用,显示分割的元素。 95 v=test.partition("d") #输出结果(‘kj‘, ‘d‘, ‘flkajdsfiejk‘)按照规定的字符从左或者从右开始,只能分2次! 96 # v=test.rpartition(" ") #不能是空字符,可以是空格。 97 #print(v) #输出结果(‘‘, ‘‘, ‘kjdflkajdsfiejk‘) 98 99 #test="djskfhjhejujdsfh" 100 #v=test.split("j") #[‘d‘, ‘skfh‘, ‘he‘, ‘u‘, ‘dsfh‘]按照要求从左边开始去做分割,但是这个元素是不会显示的。 101 #v=test.split("j",3) #[‘d‘, ‘skfh‘, ‘he‘, ‘ujdsfh‘] 按照要求去分割,但是只能分割次数由后面的数字限定) 102 #v=test.rsplit("j",2) # [‘djskfhjhe‘, ‘u‘, ‘dsfh‘] 按照要求从右边开始去做分割,分割次数由后面的数字限定。 103 104 #正则表达式 105 #是否想要分割的元素,有自己决定。 106 #以后再讲。 107 108 #分割,只能根据换行符分割。 109 #test="dafhiuh\njwke\ndjfej\nhjmnjh" 110 #v=test.splitlines() #[‘dafhiuh‘, ‘jwke‘, ‘djfej‘, ‘hjmnjh‘] 不显示换行符。 111 #v=test.splitlines(False) #[‘dafhiuh‘, ‘jwke‘, ‘djfej‘, ‘hjmnjh‘] 也不显示换行符 112 #v=test.splitlines(True) #[‘dafhiuh\n‘, ‘jwke\n‘, ‘djfej\n‘, ‘hjmnjh‘]显示换行符 113 114 #test="dskbeil93" 115 #v=test.startswith("d") #判断是否以指定字符开头,并给出结果:False & True 116 #v=test.endswith("3") #判断是否以指定字符结尾,并给出结果:False & True 117 118 119 # test="alQEFDcai" 120 # v=test.swapcase() #ALqefdCAI大小写转换(大写变小写,小写变大写)
原文:https://www.cnblogs.com/ningbotianjiao/p/12398324.html