An example of how to use the split() function in Python

Summary:

This article mainly introduces the use of the split() function in Python. The use of the split() function is the basic knowledge in Python learning. It is usually used to slice and convert strings into lists. Friends who need it can refer to Down.

Function: split()

There are two functions, split() and os.path.split(), in Python. The specific functions are as follows:
split(): Split strings. Slice the string by specifying the delimiter, and return the split string list (list)
os.path.split(): split the file name and path according to the path

1. Function description

1. split() function

Syntax: str.split(str="",num=string.count(str))[n]

Parameter description:
str: It is expressed as a separator, the default is a space, but it cannot be empty (''). If there is no separator in the string, the entire string is taken as an element of the list.
num: indicates the number of divisions. If the parameter num exists, it is only divided into num+1 substrings, and each substring can be assigned to a new variable
[n]: Indicates that the nth slice is selected

Note: When spaces are used as separators, items that are empty in the middle are automatically ignored

2. os.path.split() function
Syntax: os.path.split('PATH')

Parameter Description:

  • PATH refers to the full path of a file as an argument:
  • If a directory and filename are given, output the path and filename
  • If a directory name is given, the output path sum is an empty file

 Second, example
1, common examples

u = "www.doiido.com.cn"

#Use the default delimiter 
print (u.split())
[ ' www.doiido.com.cn ' ]

#With "." as the separator 
print (u.split( ' . ' ))
[ ' www ' , ' crazy ' , ' com ' , ' cn ' ]

#Split 0 times 
print (u.split( ' . ' ,0))
[ ' www.doiido.com.cn ' ]

#Split once 
print (u.split( ' . ' ,1 ))
[ ' www ' , ' doiido.com.cn ' ]

#Split once, take the previous character 
print (u.split( ' . ' ,1 )[0])
www

#Split once, take the following characters 
print (u.split( ' . ' ,1)[1 ])
doiido.com.cn

#Split the most times (actually the same as without the num parameter) 
print (u.split( ' . ' ,-1 ))
[ ' www ' , ' crazy ' , ' com ' , ' cn ' ]

#Split twice, and save the divided three parts to three files 
u1, u2, u3 = u.split( ' . ' ,2 )
 print (u1)
www
print(u2)
doiido
print(u3)
com.cn

2. Remove line breaks

c = '''say
hello
baby'''
print (c.split('\n'))
['say', 'hello', 'baby']

3. Separate file name and path

import os
 #split /dodo/soft/python/ to /dodo/soft/python and empty path 
print (os.path.split( ' /dodo/soft/python/ ' ))
('/dodo/soft/python', '')

#split /dodo/soft/python to /dodo/soft and python 
print (os.path.split( ' /dodo/soft/python ' ))
('/dodo/soft', 'python')

 

4. A super good example

str = "hello boy<[https://www.cnblogs.com/forcheny]>byebye"
print (str.split("[")[1].split("]")[0].split("."))
[ ' https: // www ' , ' cnblogs ' , ' com / forcheny ' ]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305108&siteId=291194637