[Tips] using the Python split () method of the string sections

split() The method can be divided according to a specified string delimiter into a plurality of sub-strings, the string will be saved to the sub-list (excluding separator)

split() Method syntax:str.split(sep,maxsplit)

str : the string to be split

On Sep : specifies the separator may comprise a plurality of characters (e.g., '1<>2<>3'.split('<>')returns ['1', '2', '3']). This parameter defaults None, showing all blank characters, including spaces, newline \ n-, tab \ t like

maxsplit is : an optional parameter that specifies the number of division (and therefore, there will be a list of most elements maxsplit + 1), if not specified, or specified as -1 indicates that the number of divisions is not limited.

For example:

>>> '1 2 3 4'.split()
['1', '2', '3', '4']

>>> '1,2,3'.split(',')
['1', '2', '3']

>>> '1,2,3'.split(',', 1)
['1', '2,3']

>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']

>>> '1<<20<<300'.split('<<')
['1', '20', '300']

The special little, if no sepparameter, when there is a continuous string of blank spaces or other characters, will be treated as a string delimiter is divided

>>> '1 2    3 4  5'.split()
['1', '2', '3', '4', '5']
Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104381710