Home  >  Article  >  Backend Development  >  Does python have a split function?

Does python have a split function?

anonymity
anonymityOriginal
2019-06-15 14:05:363382browse

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

Does python have a split function?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn