Detailed explanation of how to use strip() and split() in python

Y2J
Release: 2017-05-18 14:31:11
Original
3584 people have browsed it

This article mainly introduces the detailed explanation and examples of the python strip() function and split() function. Friends in need can refer to

python strip() function and split( ) Detailed explanation and examples of functions

I have never been able to distinguish the functions of strip and split. In fact, strip means delete; and split means splitting. Therefore, it also means that the two functions are completely different. Strip can delete certain characters of string, while split splits the string according to the specified characters. Let’s talk about these two functions in detail below.

1 Python strip() function introduction

Function prototype

Declaration: s is a string, rm is the character sequence to be deleted

s.strip(rm) Delete the characters at the beginning and end of the s string, located in the rm deletion sequence

s.lstrip(rm) Delete the s character The character at the beginning of the string, located in the rm deletion sequence

s.rstrip(rm) Delete the character at the end of the s string, located in the rm deletion sequence

Note:

(1) When rm is empty, whitespace characters (including '\n', '\r', '\t', ' ') are deleted by default.

(2)Here The rm deletion sequence is to delete the characters on the edge (beginning or end) as long as they are within the deletion sequence.

For example,

>>> a = '  123' 
>>> a 
'  123' 
>>> a.strip() 
'123'
Copy after login

(2) The rm deletion sequence here is to delete the characters on the edge (beginning or end) as long as they are within the deletion sequence.

For example,

>>> a = '123abc' 
>>> a.strip('21') 
'3abc' 
>>> a.strip('12') 
'3abc'
Copy after login

The result is the same.

2 python split() function introduction

Explanation:

There is no character type in Python In other words, there is only the string . The character mentioned here is a string containing only one character! ! !

The reason why it is written like this is just for the convenience of understanding, nothing more.

(1) Split by a certain character, such as '.'

>>> str = ('www.google.com') 
>>> print str 
www.google.com 
>>> str_split = str.split('.') 
>>> print str_split 
['www', 'google', 'com']
Copy after login

(2) Split by a certain character, and split n times. For example, press '.' to split once

>>> str_split = str.split('.',1) 
>>> print str_split 
['www', 'google.com']
Copy after login

(3) You can also add regular expression after the split() function, for example:

>>> str_split = str.split('.')[0] 
>>> print str_split 
www
Copy after login

after split is a List, [0] means taking the first element;

>>> str_split = str.split('.')[::-1] 
>>> print str_split 
['com', 'google', 'www'] 
>>> str_split = str.split('.')[::] 
>>> print str_split 
['www', 'google', 'com']
Copy after login

Arrange in reverse order, [::] Arrange in positive order

>>> str = str + '.com.cn' 
>>> str 
'www.google.com.com.cn' 
>>> str_split = str.split('.')[::-1] 
>>> print str_split 
['cn', 'com', 'com', 'google', 'www'] 
>>> str_split = str.split('.')[:-1] 
>>> print str_split 
['www', 'google', 'com', 'com']
Copy after login

From the first element to the end, the last one The element is deleted.

One of the typical applications of the split() function, ip number exchange:

# ip ==> number

>>> ip2num = lambda x:sum([256**j*int(i) for j,i in enumerate(x.split('.')[::-1])]) 
>>> ip2num('192.168.0.1') 
3232235521
Copy after login

# number ==> ip # number range [0, 255^4]

>>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)]) 
>>> num2ip(3232235521) 
'192.168.0.1'
Copy after login

Finally, how does Python convert an integer to an IP address?

>>> import socket 
>>> import struct 
>>> int_ip = 123456789 
>>> socket.inet_ntoa(struct.pack(‘I',socket.htonl(int_ip)))#整数转换为ip地址 
‘7.91.205.21' 
>>> str(socket.ntohl(struct.unpack(“I”,socket.inet_aton(“255.255.255.255″))[0]))#ip地址转换为整数 
‘4294967295'
Copy after login

【Related recommendations】

1. Python free video tutorial

2. Detailed explanation of the usage scenarios of strip function in python

3. The little-known traps of strip() in python

4. The wonderful uses of strip() function in Python that you don’t know

5. Basic introduction to python teaches you how to use the strip() function to remove spaces\n\r\t

The above is the detailed content of Detailed explanation of how to use strip() and 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