Python in the strip () and split () usage

Python strip () method used to remove the head and tail of the specified character string (default is a space).

grammar

str.strip([chars]);

parameter

chars - remove the head and tail of the specified string of characters.

return value

Return new string to remove the head and tail string specified character generation.

Examples

The following example shows the strip () function using the method:

#!/usr/bin/python

= STR "String Example 0000000this IS 0000000 ... WOW !!!";
Print str.strip ( '0');
the above examples of output results are as follows:

this is string example…wow!!!
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

Python split () specified by the string sections delimiter, if the parameter value is specified num, num only separated substrings

grammar

str.split(str="", num=string.count(str)).

parameter

str - separator, default for all null characters, including spaces, linefeed (\ n-), tab (\ t) and the like.
num - the number of divisions.

return value

Returns a list of strings after division.

Examples

The following examples illustrate the split () function to use:

#!/usr/bin/python

= STR "Linel-abcdef \ nLine2-ABC \ nLine4-ABCD";
Print str.split ();
Print str.split ( '', 1); num herein designated as # 1, the split only once, a distinction
above example output is as follows:
[ 'Linel-abcdef', 'Line2-ABC', 'Line4-ABCD']
[ 'nLine2-ABC \ nLine4-ABCD \' 'Linel-abcdef',]

Reference: https://www.cnblogs.com/yunyinzhihua/p/6884920.html

Guess you like

Origin blog.csdn.net/xavier_muse/article/details/90480330