Home > Article > Backend Development > Does python have a split function?
There is the split function in python, Python split() Slice the string by specifying the delimiter. If the parameter num has a specified value, separate num 1 substrings
split() method syntax:
str.split(str="", num=string.count(str)).
Parameters
str -- separator, default is all null characters , including spaces, newlines (\n), tabs (\t), etc.
num -- Number of divisions. The default is -1, which separates everything.
Return value
Returns the split string list.
How to use the split() function:
#!/usr/bin/python # -*- coding: UTF-8 -*- str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); # 以空格为分隔符,包含 \n print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个
The output result of the above example is as follows:
['Line1-abcdef', 'Line2-abc', 'Line4-abcd'] ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
The above is the detailed content of Does python have a split function?. For more information, please follow other related articles on the PHP Chinese website!