Python Strip () method

Source: Internet
Author: User

The Python Strip () method removes the character specified by the string's Kinsoku (the default is a space).

Grammar

Strip () method syntax:

Str.  Strip([chars]);    
Parameters
    • Chars--Removes the character specified by the tail of the string.
return value

Returns a new string that is generated by removing the string from the Kinsoku specified character.

Instance

The following example shows how the strip () function is used:

#!/usr/bin/python="0000000this is string EXAMPLE....WOW!!! 0000000 ";  Print str.  Strip(' 0 ');         

The result of the above example output is as follows:

This isstring example .... WOW!!!     



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(‘21‘) ‘3abc‘>>> a.strip(‘12‘) ‘3abc‘
    • The result is the same.

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

?
12345 >>> 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

?
12345 >>> a =‘123abc‘>>> a.strip(‘21‘)‘3abc‘>>> a.strip(‘12‘)‘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 '. '

?
123456 >>> str=(‘www.google.com‘)>>> printstrwww.google.com>>> str_split =str.split(‘.‘)>>> print str_split[‘www‘‘google‘‘com‘]

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

?
123 >>> str_split =str.split(‘.‘,1)>>> printstr_split[‘www‘‘google.com‘]

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

?
123 >>> str_split =str.split(‘.‘)[0]>>> printstr_splitwww

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

?
123456 >>> str_split =str.split(‘.‘)[::-1]>>> printstr_split[‘com‘‘google‘‘www‘]>>> str_split =str.split(‘.‘)[::]>>> printstr_split[‘www‘‘google‘‘com‘]

Sorted by inverse sequence, [::] Yasumasa Order

?
123456789 >>> str=str +‘.com.cn‘>>> str‘www.google.com.com.cn‘>>> str_split =str.split(‘.‘)[::-1]>>> printstr_split[‘cn‘‘com‘‘com‘‘google‘‘www‘]>>> str_split =str.split(‘.‘)[:-1]>>> printstr_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

?
123 >>> ip2num =lambda x:sum([256**j*int(i) forj,i inenumerate(x.split(‘.‘)[::-1])])>>> ip2num(‘192.168.0.1‘)3232235521

# Digital ==> IP # Digital range [0, 255^4]

?
123 >>> num2ip =lambdax: ‘.‘.join([str(x/(256**i)%256forinrange(3,-1,-1)])>>> num2ip(3232235521)‘192.168.0.1‘

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

?
1234567 >>> importsocket>>> importstruct>>> 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

Python Strip () method

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.