Home > Backend Development > Python Tutorial > Detailed explanation of the usage of split in python

Detailed explanation of the usage of split in python

hzc
Release: 2020-06-13 15:24:12
Original
14073 people have browsed it

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

1

2

3

4

5

6

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

1

2

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']

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

Copy after login

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!

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