Constants in the 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
[HTML]View Plain Copy
- >>> Import string
- >>> String.digits
- ' 0123456789 '
- >>> String.letters
- ' Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '
- >>> String.lowercase
- ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
- >>> String.printable
- ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! ' #$%&\ ' () *+,-./:; <=>[email protected][\\]^_ ' {|} ~ \t\n\r\x0b\x0c '
- >>> String.punctuation
- ‘!" #$%&\ ' () *+,-./:; <=>[email protected][\\]^_ ' {|} ~ '
- >>> String.uppercase
- ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
1. Find function
Queries a substring in a longer string, returns the leftmost index where the substring is located, and no return-1 is found
[HTML]View Plain Copy
- >>> title = "Monty Python ' s Flying Circus"
- >>> Title.find (' Monty ')
- 0
- >>> Title.find (' Monty ')
- -1
You can select a starting point and an end point
[HTML]View Plain Copy
- >>> Title.find (' Python ')
- 6
- >>> Title.find (' Python ', 3)
- 6
- >>> Title.find (' Python ', 3, ten)
- -1
2. Join function
Adding elements to a queue (only works on strings, returns a modified string, but the original string does not change)
[HTML]View Plain Copy
- >> 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
Inverse method: Split function
Splits a string into a sequence, returns the sequence, and the original string does not change
[HTML]View Plain Copy
- >>> Word = ' 1+2+3+4+5 '
- >>> Word.split (' + ')
- [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
- >>> Word
- ' 1+2+3+4+5 '
3. Lower function
Returns the lowercase master of a string
[HTML]View Plain Copy
- >>> ' fafdawdfawedwed '. Lower ()
- ' Fafdawdfawedwed '
Extended:
Title function: Capital Letter, other lowercase
[HTML]View Plain Copy
- >>> "That's All Folks". Title ()
- "That's all folks"
Capwords function: function as above, function in string module
[HTML]View Plain Copy
- >>> Import string
- >>> String.capwords ("That's All Folks")
- "That's all folks"
4. Replace function
Returns the string from which all occurrences of a string are replaced, without changing the original string
[HTML]View Plain Copy
- >>> Word = ' This is a test '
- >>> Word.replace (' Is ', ' EEZ ')
- ' Theez eez a test '
- >>> Word
- ' This is a test '
Maketrans function: function above, conversion table in string, total 256 items, function accepts 2 equal length string, each character in the first string is replaced by character in the corresponding position in the second string
Maketrans is similar to a rule that is often combined with translate to complete a string substitution that cannot be accomplished by some ordinary function
[HTML]View Plain Copy
- >> > from string import maketrans
- >>> table = maketrans (' cs ', ' KZ ')
- >>> len (table)
- 256
- > >> table[97:123]
- ' abkdefghijklmnopqrztuvwxyz '
- >>> maketrans (', ') [ 97:123]
- ' abcdefghijklmnopqrstuvwxyz '
Translate function: function as above, but can only handle a single character, there are 2 parameters, the first is a replacement, the second is to delete
Example: Table in a table successor Maketrans
[HTML]View Plain Copy
- >>> ' This was an incredible test '. Translate (table)
- ' Thiz iz an inkredible tezt '
- >>> ' This was an incredible test '. Translate (table, ')
- ' Thizizaninkredibletezt '
5. Strip function
Remove strings from both sides (excluding inner) spaces, the original sequence is unchanged
[HTML]View Plain Copy
- >>> Word = ' This is test '
- >>> Word.strip ()
- ' This is test '
- >>> Word
- ' This is test '
You can add a parameter to the strip () to remove the specified character that you want to remove
[HTML]View Plain Copy
- >>> ' * * * * SPAM * for * everyone!!! '. Strip (' * ')
- ' SPAM * for * everyone!!! ‘
- >>> ' * * * * SPAM * for * everyone!!! '. Strip (' * ')
- ' SPAM * for * everyone!!! '
- >>> ' * * * * SPAM * for * everyone!!! '. Strip (' *! ')
- ' SPAM * for * everyone '
Python: String Functions