strip () function

 

description

Python strip () method for removing head and tail of the specified character string (line feed spaces or default) or character sequence.

Note: This method can only delete the beginning or end of the character, the character can not be deleted middle part.

 

usage:

str.strip([chars]);

 

The above is the description of the function strip novice tutorial content for the removed bit puzzled and an intermediate portion, following a number of examples

Example 1:

 

 s_1 = 'a123a'

For the string 'a1231', the removal of the head and tail of 'a', it is apparent to get '123'

Example 2

 

 s_2 = 'abc123abc'

 Through the above two examples, my initial guess strip head and tail at the same time is a function to specify the number of bits required to remove the string to scan a string, to match string consistent with the specified string to be deleted

1 is for example, a head and tail S_1 a traverse, the first character of the head and tail of the character need to go out (a) the same, is obtained after removing 123

For example for 2, 3 S_2 craniocaudal three characters traverse three characters, both the head and tail of the string (ABC) is the same need to be removed, after removing 123 is obtained

In my initial ideas to explain Example 1, Example 2, seems to be like this ... Well, the idea is correct it? Take a look at Example 3

Example 3:

 

 

 

 

 

 

I had to guess, I think that the above results are 123 *, abc * 123, bc * 123 *, c * 123 * ab

However, their output actually 123

Consistent with previous speculation are matched by this example, I guess again: the head and tail removed at the same time to specify the number of bits required to scan string string, as to match the specified string, without a position on the same string deleted

Then think of it again, one by one while scanning the head and tail, as long as it contains the deleted character to be deleted in the specified string. This statement can be interpreted through the contents of deleted

Then, "the middle of the content of" how to judge it? View Example 4

Example 4

 

  To guess what the output should do?

正确的输出内容是:*1ab23ab*c

下面来详解一下如何得到这个结果:

分别从头部和尾部开始扫描,头部的a和尾部的b包含在ab中,则会去掉头部的a,尾部的b

-------得到bab*1ab23ab*caabab

第二次扫描,头部的b和尾部的b都包含在ab中,去掉

-------得到ab*1ab23ab*caaba

第三次扫描,头部的a和尾部的a都包含在ab中,去掉

-------得到b*1ab23ab*caab

第四次扫描,头部的b和尾部的b都包含在ab中,去掉

-------得到*1ab23ab*caa

第五次扫描,头部的*不包含在ab中,不去掉,尾部的a包含在ab中,去掉

-------得到*1ab23ab*ca 

第六次扫描,头部的*不包含在ab中,不去掉,尾部的a包含在ab中,去掉

-------得到*1ab23ab*c

第七次扫描,头部的*不包含在ab中,不去掉,尾部的c不包含在ab中,不去掉

-------得到*1ab23ab*c

则从以上推测中,我们可以知道,遇到不满足的字符则扫描结束,即从遇到不满的字符开始就算中间部分了

 

以上只是我学习strip函数时的所想所思,根据我的这个想法,我自己写了一个与strip有相同用法的函数

 

 

 

有错误或者别的想法,请大家多多指教。联系qq1435655338

Guess you like

Origin www.cnblogs.com/miao233/p/12098333.html