Home>Article>Backend Development> Detailed explanation of the usage of split in python
Detailed explanation of the usage of split in python:
split() function
Syntax: str.split(str="",num=string.count(str))[n]
Parameter description:
str: expressed as a separator, the default is a space, but Can not be empty(''). If there is no delimiter in the string, the entire string is treated as an element of the list
num: indicates the number of divisions. If the parameter num exists, it will only be divided into num 1 substrings, and each substring can be assigned to a new variable
[n]: means selecting the nth fragment
Note: When When using spaces as separators, empty items in the middle will be automatically ignored
#!/usr/bin/python # -*- coding: UTF-8 -*- str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); # 以空格为分隔符,包含 \n print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个
The output results of the above examples are as follows:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
Recommended tutorial: "Python Tutorial》
The above is the detailed content of Detailed explanation of the usage of split in python. For more information, please follow other related articles on the PHP Chinese website!