Constants in the 1.String module:
String.digits: Digital 0~9
String.letters: All Letters (case)
String.lowercase: All lowercase letters
String.printable: A string that can print characters
String.punctuation: All punctuation
String.uppercase: All uppercase letters
Import string>>> string.uppercase ' abcdefghijklmnopqrstuvwxyz ' >>> string.lowercase ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> string.digits ' 0123456789 ' >>> string.letters ' Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ' >>> string.printable ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! " #$%&\ ' () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ \t\n\r\x0b\x0c ' >>> string.punctuation '! " #$%&\ ' () *+,-./:;<=>[email protected][\\]^_ ' {|} ~
The 2.find function queries substrings in a longer string, returns the leftmost index where the substring is located, and does not find return-1
>>> title = "Monty Python ' s Flying Circus" >>> title.find (' Monty ') 0 >>> Title.find (' Monty ') -1 can choose Start point and end point >>> title.find (' Python ') 6 >>> title.find (' Python ', 3) 6 >>> title.find (' Python ', 3, Ten) -1
The 3.join function adds an element to the queue (only works on a string, returns a modified string, but the original string does not change), its inverse function splits the string into a sequence for the Split,split function, returns the sequence, and the original string does not change
>>> seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '] >>> Sep = ' + ' >>> sep.join (seq) ' 1+2+3+4+5 '
>>> seq [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '] >>> dirs = ', ' usr ', ' bin ', ' env ' >>> '/'. Join (dirs) ' /usr/bin/env ' >>> print ' C: ' + ' \ \ '. Join (dirs) C:\usr\bin\env >>> word = ' 1+2+3+4+5 ' >>> word.split (' + ') [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '] >>> word ' 1+2+3+4+5 '
4.lower function returns the lowercase master of a string
>>> ' fafdawdfawedwed '. Lower () ' fafdawdfawedwed ' extension: Title function: Capital letter, other lowercase >>> "That's all" Folks ". Title ()
The 5.replace function returns a string where all occurrences of a string have been replaced, and the original string does not change
>>> Word = ' This was a test ' >>> word.replace (' is ', ' EEZ ') ' theez eez a test ' >>> wo Rd ' This is a test
6.strip function to remove strings of both sides (excluding interior) spaces, the original sequence is unchanged
>>> Word = ' This was test ' >>> word.strip () ' This is test ' >>> Word ' This is Test ' can add parameters in strip () to remove the specified character that you want to remove >>> ' * * * SPAM * for * everyone!!! '. Strip (' * ') ' SPAM * for * everyone!!! ' >>> ' * * * SPAM * for * everyone!!! '. Strip (' * ') ' SPAM * for * everyone!!! '
"Python" string function