Python split () method for dividing the string

Python, in addition to some of the built-in functions may be used to obtain information string (e.g. len () function to get the length of the string), the string type itself has methods for our use.

Note that the methods mentioned here, refers to the type of string str itself provides, as it relates to classes and objects of knowledge, beginners do not have to get to the bottom, 
just need to know the specific method to use.

split () method can be implemented into a plurality of sub-strings of a string delimiter as specified cut, sub-string will be saved to the list (excluding separator), as the return value is fed back. The basic syntax of the method is as follows:

str.split(sep,maxsplit)

This method Meanings of some parameters are:

str: string indicates to split;

sep: specifies the separator may comprise a plurality of characters. This parameter defaults to None, represents all null characters, including spaces, line breaks, "\ n", tab "\ t" and so on.

maxsplit: an optional parameter that specifies the number of division, the number of the last list of strings most neutron maxsplit + 1. If not specified, or specified as -1 indicates that the number of divisions is not limited.

In the split method, if you do not specify sep argument, nor can it specify maxsplit parameters.

Unlike the built-in function calls (such as len) of use, the method string variables have, only a "string method name ()" method. Why do not tangle here, after completing school classes and objects, it will naturally understand.

For example, the definition of a network storage php Chinese URL string, and then separated by split () method depending on the separator, the implementation process is as follows:

>>> str = "php Chinese network c.biancheng.net >>>" 
>>> STR 
'PHP Chinese network c.biancheng.net >>>' 
>>> str.split List1 = () # default delimiter dividing 
>>> List1 
[ 'PHP Chinese network', '>>>', 'c.biancheng.net'] 
>>> List2 = str.split ( '>>>') using a plurality of characters divided # 
> List2 >> 
[ 'PHP Chinese network', 'c.biancheng.net'] 
>>> list3 = str.split ( '.') # adopted. No. dividing 
>>> list3 
[ 'PHP Chinese network >>> c ',' biancheng ',' nET '] 
>>> list4 = str.split (' ', 4) using # space is divided, and only up to a predetermined divided into four sub-string 
>>> list4 
[' PHP Chinese network ' , '>>>', 'c.biancheng.net '] 
>>> list5 = str.split ('> ') using #> characters segmentation 
>>> list5 
[' PHP Chinese network ',' ',' ',' c.biancheng.net '] 
> >>

Note that, when the parameter is not specified sep, Split () method defaults to null character is divided, but with a continuous string blank spaces or other characters, will be treated as a string delimiter is divided ,E.g:

>>> str = "php Chinese network >>> c.biancheng.net" # 3 consecutive spaces comprising 
>>> list6 = str.split () 
>>> list6 
[ 'PHP Chinese network', '>>> ',' c.biancheng.net '] 
>>>

This article is reproduced in https://www.py.cn/jishu/jichu/10831.html

Guess you like

Origin www.cnblogs.com/jsdd/p/11613121.html