What is split in python

藏色散人
Release: 2019-06-24 11:14:45
Original
15663 people have browsed it

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)).
Copy after login

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 ); # 以空格为分隔符,分隔成两个
Copy after login

The output results of the above example are as follows:

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

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
Copy after login

The output results of the above example are as follows:

['Google', 'Runoob#Taobao#Facebook']
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!