python string modification, the slicing operation

1. python string belong immutable object does not support the modification in place, if you need to modify these values, only to re-create a new string object.

2 can be converted into a string list, edit list, and then to use the join list is converted to STR , to achieve modified string operation .

3. can replace () replacement portion of the sub-strings to achieve the purpose of the modification portion.

4. python slicing operation is an important sequence for list, string, and other objects, using two sections separated by colons three digital representation (start: end: step) may be sliced ​​using any part of the list of intercepted, You can modify the list by slice.

Examples of computing expression parameter is the number of code snippets

= Word " F. (G (A, A), F (AA, BB)) " 
Word = word.replace ( "  " , "" )   # remove spaces string 
N1 = word.rfind ( " ( " )     # rightmost left bracket 
N2 = word.find ( " ) " , N1)   # corresponding right parenthesis 
S1 Word = [N1 +. 1: N2]       # read character string in brackets, i.e., the innermost layer parameters aa, bb 
Pass    # other processing 
l_word = list (Word)      # string transfer list 
l_word [N1: N2 +. 1] = []     # use list slicing operation and, in parentheses, and the parameter clear 
Word = '' .join (l_word)   # join usingTo String list, F (g (a, a), f)

 

Guess you like

Origin www.cnblogs.com/imhuanxi/p/11106719.html