Common string constants:
string . Digits : Contains numbers 0~9 the string
string . Letters : Contains all letters (uppercase or lowercase strings, python3.0 , use string.ascii-letters instead)
string . Lowercase : A string containing all lowercase letters
string . Printable : A string containing all printable characters
string . Punctuation : A string that contains all the punctuation
string . Uppercase : A string containing all uppercase letters
1 ) Find : Finds a substring in a longer string, returns the leftmost index where the substring is located, and returns if not found - 1
>>>title = "Monty Python ' s Flying Circus"
>>>title.find (' Monty ')
0
>>>title.find (' Python ')
6
Accept the optional start and end points parameters:
>>>subject = ' $$$ Get rich now!!! $$$ '
>>>subject.find (' $$$ ', 1)
21st
>>>subject.find ('!!! ', 1,16)
-1
2 ) Join : Used to concatenate elements in a sequence, and must be a string, syntax format: Connector . Join ( string )
>>>seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>>sep = ' + '
>>>sep.join (seq)
' 1+2+3+4+5 '
>>>dir = ', ' usr ', ' bin ', ' env '
>>> '/'. Join (dir)
'/usr/bin/env '
3 ) Lower : Returns the lowercase letter of a string
>>> ' Trondheim Hammer Dance '. Lower ()
' Trondheim Hammer Dance '
>>>
>>>
>>> name = ' Gumby '
>>> names = [' Gumby ', ' Smith ', ' Jones ']
>>> if Name.lower () in Names:print ' Found it '
...
Found it
Title Conversion:
title method: Capitalize all the words in the first letter, while the other letters are lowercase
>>> "That's All Folks". Title ()
"That ' SAll folks"
>>>
Capword function:
>>>import string
>>>string.capwords ("That's All Folks")
"That ' sAll folks"
4 ) Replace : Returns the string after all occurrences of a string have been replaced, that is, the lookup substitution
>>> ' This was a test '. Replace (' is ', ' EEZ ')
' Theezeez a test '
5 ) Split : Join inverse method used to split a string into a sequence
>>> ' 1+2+3+4+5 '. Split (' + ')
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
6 ) Strip : Returns a string that strips both sides (with no interior) space (by default)
>>> ' internal whitespace krpt '. Strip ()
' Internalwhitespace krpt '
Specify delimiter:
>>> ' * * * SPAM * for * everyone!!! '. Strip (' *! ')
' spam* for * everyone '
7 ) Translate : Replacing portions of a string with only a single character, with the advantage that you can make multiple substitutions at the same time, before replacing it, you need to complete a conversion table and use string module's Maketrans function
>>>from String Import Maketrans
>>>table = Maketrans (' cs ', ' KZ ')
>>>len (table)
256
>>> ' This was an incredibletest '. Translate (table, ') # The second parameter is optional, specifying the character to be deleted
' Thizizaninkredibletezt '
Python Learning Note Five: string methods