Article From:https://www.cnblogs.com/python1988/p/9971803.html
test="abcdeffedcba"
v=test.lstrip("bcabc")#Find the maximum match and remove the specified string
print("1.lstripUsage 3 to find the most matches and remove the specified characters","\n",v)
test1="Silly ABC"
test2="Who is 123?"
test3="You are all fools, abcefgh321"
a=str.maketrans(test1,test2)  #This STR does not understand why to write str
b=test3.translate(a) #maketrans Make the corresponding relationship with translate first, and then replace the new statement with the new corresponding relationship.
print("2.1:maketrans Make the correspondence by law, translate and replace:","\n",b)
test4="Life is like a play, it's all about acting."
c=str.maketrans("Drama is like life","You are an idiot")# Making Transformational Relationships with Maketrans
d=test4.translate(c)
print("2.2:First confirm the statement to be converted, then confirm the transformation relationship, and finally use trasnlate to generate the final conversion statement:","\n",d)
test="sxjsxjsxj"
e=test.partition("x")# Divide the above statement into three parts, then use X to find the first X and do isolated segmentation. This should be left segmentation.
f=test.rpartition("j") #The statement is split right, and if it is split into the first word, the output is empty.
print("3.partitionThe function is to divide into three parts, and the rpartition is the same:","\n",e,"\n",f)
g=test.split("s",2)#The maximum number of times is 2. Note that s has been removed when segmentation.
h=test.rsplit("x",2)
print("4.split And rsplit for the specified number of times of partitioning:","\n",g,"\n",h)
test="abc\ndef\nghi\njkl\nmno"
i=test.splitlines()#You can only divide it by line break \ n, and fill in True and False in the blanks (if not, it means false) whether to keep line breaks or not.
j=test.splitlines(True)
print("5.splitlines Segmentation based on line breaks:","\n",i,"\n",j)
k=test.startswith("a")#Specifies the beginning of a string
l=test.endswith("j")#Specifies the end of the string
print("6.startswithAnd endswith usage to determine whether a specified string begins or ends:",k,l)
m=test1.swapcase()#toggle case
print("7.Case-to-case swapcase:","\n",m)

 

Leave a Reply

Your email address will not be published. Required fields are marked *