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‘
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‘
)
>>>
print
str
www.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 ) >>> print str_split [ ‘www‘ , ‘google.com‘ ] |
(3) after the split () function, you can also add a regular expression, for example:
?
123 |
>>> str_split = str .split( ‘.‘ )[ 0 ] >>> print str_split www |
Split is a list, [0] that takes its first element;
?
123456 |
>>> 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
?
123456789 |
>>>
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
?
123 |
>>> 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]
?
123 |
>>> 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?
?
1234567 |
>>>
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
‘
|
Python Strip () method