Does python have a split function?

anonymity
Release: 2019-06-15 14:05:36
Original
3400 people have browsed it

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

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

The output result of the above example is as follows:

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

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!

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