Python strip () function and split () function details and examples, pythonstrip

Source: Internet
Author: User

Python strip () function and split () function details and examples, pythonstrip

Details and examples of python strip () and split () Functions

The strip and split functions are unclear for a long time. In fact, strip means deletion, while split means segmentation. Therefore, the two functions are completely different. strip can delete some characters of a string, while split splits the string according to the specified characters. The two functions are described in detail below,

1 Python strip () function Introduction

Function prototype

Declaration: "s" is a string and "rm" is the sequence of characters to be deleted.

S. strip (rm) Delete the characters starting and ending in the s string and located in the rm Delete Sequence

S. lstrip (rm) Delete the characters starting from the s string in the rm Delete Sequence

S. rstrip (rm) deletes the characters at the end of the s string in the rm deletion sequence.

Note:

(1) When rm is blank, blank spaces (including '\ n',' \ R', '\ t', '') are deleted by default ','')

(2) Here, the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are in the delete sequence.

For example,

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

(2) Here, the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are in the delete sequence.

For example,

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

The results are the same.

2. Introduction to the python split () function

Note:

Python has no character type, only stringsThe character here is a string that only contains one character !!!

The reason for writing this is only for ease of understanding.

(1) split by a certain character, such '.'

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

(2) It is separated by a certain character and divided n times. For example, split one time '.'.

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

(3) The split () function can be followed by a regular expression, for example:

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

Split is a list. [0] indicates that the first element is obtained;

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

Sort by reverse sequence, [:] in ascending 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'] 

Delete the last element from the first element to the end of the next element.

One of the typical applications of the split () function, ip address digital interchange:

# Ip => Number

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

# 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' 

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) # convert an integer to an IP address '7. 91.205.21 '>>> str (socket. ntohl (struct. unpack ("I", socket. inet_aton ("255.255.255.255") [0]) # convert an IP address to an integer '123'

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.