Home >Backend Development >Python Tutorial >What is split in python

What is split in python

藏色散人
藏色散人Original
2019-06-24 11:14:4515721browse

What is split in python

What is split in python?

split() in python slices the string by specifying the delimiter. If the parameter num has a specified value, num 1 substring is separated.

split( ) Method syntax:

str.split(str="", num=string.count(str)).

Parameters

str -- delimiter, default is all empty characters, including spaces, newlines (\n), tabs (\t) wait.

num -- Number of divisions. The default is -1, which separates everything.

Return value

Returns the split string list.

The following example shows how to use the split() function:

Example (Python 2.0)

#!/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 example are as follows:

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

The following example uses # as the separator, specifies the second parameter as 1, and returns two parameter lists.

Example (Python 2.0)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
txt = "Google#Runoob#Taobao#Facebook"
 
# 第二个参数为 1,返回两个参数列表
x = txt.split("#", 1)
 
print x

The output results of the above example are as follows:

['Google', 'Runoob#Taobao#Facebook']

Related recommendations: "Python Tutorial"

The above is the detailed content of What is split in python. 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