How to use Strip () and split () in Python

Source: Internet
Author: User
This article mainly introduces the Python strip () function and the split () function of the details and examples of relevant information, the need for friends can refer to the following

A detailed and practical example of the Python strip () function and the split () function

The function of strip and split is not clear all the time, in fact strip is the meaning of deletion, while split is the meaning of division. It also indicates that the two functions are completely different, strip can delete some characters of the string, and split splits the string according to the specified character. Here's a detailed look at these two features,

1 description of the Python strip () function

Function prototypes

Declaration: S is a string, RM is a sequence of characters to be deleted

S.strip (RM) Remove the characters from the beginning and end of the s string in the RM delete sequence

S.lstrip (RM) Delete the character in the S string at the beginning of the RM delete sequence

S.rstrip (RM) Removes the character from the end of the S string that is located in the RM delete sequence

Attention:

(1) When RM is empty, the default is to remove the white space character (including ' \ n ', ' \ R ', ' \ t ', ')

(2) The RM delete sequence here is deleted as long as the characters on the edge (beginning or end) are deleted in the sequence.

For example

>>> a = '  123 ' >>> a '  123 ' >>> a.strip () ' 123 '

(2) The RM delete sequence here is deleted as long as the characters on the edge (beginning or end) are deleted in the sequence.

For example

>>> a = ' 123abc ' >>> a.strip ' 3abc ' >>> a.strip (' + ') ' 3ABC '

The result is the same.

2 description of the Python split () function

Description

There is no character type in Python, only a string , the character here is a string that contains only one character!!!

The reason for writing here is just for the convenience of understanding, that's all.

(1) Divide by a character, such as '. '

>>> str = (' www.google.com ') >>> print str www.google.com >>> str_split = Str.split ('. ') >&G t;> print str_split [' www ', ' google ', ' com ']

(2) Divide by one character and divide n times. If you press '. ' Split 1 times

>>> str_split = Str.split ('. ', 1) >>> print str_split [' www ', ' google.com ']

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

>>> str_split = Str.split ('. ') [0] >>> print str_split www

Split is a list, [0] that takes its first element;

>>> str_split = Str.split ('. ') [:: -1] >>> print str_split [' com ', ' Google ', ' www '] >>> str_split = Str.split ('. ') [::] >>> print str_split [' www ', ' google ', ' com ']

Sorted by inverse sequence, [::] Yasumasa 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 ']

The last element is removed from the beginning of the first element to the end of the second.

Split () function is one of the typical applications, IP digital interchange:

# IP ==> Digital

>>> ip2num = Lambda x:sum ([256**j*int (i) for j,i in Enumerate (X.split ('. ') [::-1]]) >>> ip2num (' 192.168.0.1 ') 3232235521

# Digital ==> IP # Digital 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 '

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

>>> Import Socket >>> import struct >>> int_ip = 123456789 >>> Socket.inet_ntoa (struc T.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 '

"Recommended"

1. Python Free video tutorial

2. Detailed usage scenarios for strip functions in Python

3. Python strip () little-known traps

4. The magic of the Strip () function that you do not know in Python

5. How to get started with Python basics how to use the Strip () function to go to a space \n\r\t

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.