Python tips on how to use strip

Source: Internet
Author: User
This article is to share the content of Python in the strip of the use of methods of small coup, has a certain reference value, the need for friends can refer to

"Hors d ' oeuvres"

When it comes to Python's strip method, presumably anyone who touches Python knows it is primarily used to remove spaces. There are two ways to achieve this.

Method One: Using built-in functions

#<python>if __name__ = = ' __main__ ':    str = ' Hello world '    print ' [%s] '%str.strip () #</python>

Method Two: Call the method in the string module

#<python>import stringif __name__ = = ' __main__ ':    str = ' Hello world '    print ' [%s] '%string.strip (str) #< /python>

Do you know what the difference is between the two types of calls? Here are some personal views

Østr.strip () is a built-in function that calls Python, String.strip (str) is a method called in the string module

Østring.strip (str) is defined in the string module. and Str.strip () is defined in the Builtins module.

question one: how do I see if a method in a module is defined in a built-in module?

Use DIR (module name) to see if there is a ' __builtins__ ' attribute.

Example: Viewing the string module

#<python>print dir (String) #</python>

question two, how to view all the built-in functions in Python

#<python> Print dir (sys.modules[' __builtin__ ') #</python>

question three , how to view built-in function definitions in built-in modules

#<python>printhelp (__builtins__) #</python>

Some of the above are known to everyone, and then enter the subject of this article:

"Hard Food in rice"

First of all, please look at the following program operation results:

#<python>if __name__ = = ' __main__ ':    str = ' Hello World '     print str.strip (' hello ')    print Str.strip (' Hello '). Strip ()    print Str.strip (' Heldo '). Strip ()   #sentence 1       stt = ' h1h1h2h3h4h '    print Stt.strip (' H1 ')               #sentence 2       s = ' 123459947855aaaadgat134f8sfewewrf7787789879879 '    print s.strip (' 0123456789 ')        #sentence 3#</python>

The results are shown on the following page:

Operation Result:

Worldworldwor2h3h4aaaadgat134f8sfewewrf

Have you got the right answer? O (∩_∩) o~

If you are right, here I have 32 likes ...

Results Analysis:

First we look at the strip source code in the string module:

#<python># Strip leading and trailing tabs and Spacesdef Strip (S, chars=none): "" "    Strip (S [, chars]), string    Return A copy of the string swith leading and trailing    whitespace removed.    If chars is given and not none,remove characters in chars instead.    If chars is Unicode, S would beconverted to Unicode before stripping. ""    Returns.strip (chars) #</python>

Take the liberty of translating: This method is used to remove the trailing spaces and tab. Returns a copy of the S string minus the space. If the parameter chars does not have a value for none, then all characters appearing in the chars are removed. If chars is unicode,s, it is converted to Unicode before the operation.

Here is a description of the Sentence1 \2 \3 in lining above:

#<python>str = ' Hello World ' print str.strip (' Heldo '). Strip () #</python> Result:wor execution steps: Elloworldlloworldoworldoworl worl worwor

Specific code Execution process:

#<python>    Print Str.strip (' h ')    print str.strip (' h '). Strip (' E ')    print str.strip (' h '). Strip (' E '). Strip (' l ')    print str.strip (' h '). Strip (' E '). Strip (' L '). Strip (' d ')    print str.strip (' h '). Strip (' E '). Strip (' L '). Strip (' d '). Strip (' O ') print str.strip (' h '). Strip (' E '). Strip (' L '). Strip (' d '). Strip (' O '). ('    l '    ) Printstr.strip (' h '). Strip (' E '). Strip (' L '). Strip (' d '). Strip (' O '). Strip (' L '). Strip () #</python>

I do not know whether you understand the mystery, I was in the project manager Shaanxi courageously helped to discover this law.

Now a little summary:

S.strip (chars) Usage rules:

First, traverse the first character in the chars to see if it is in the end position in S, and if so, remove it. Set the removed new string to S, and continue looping, starting with the first character in the chars. If not, start directly from the second character of chars. Loops until the end of the characters in S is not in chars, and the loop terminates.

Key point: see if the characters in the chars are in S

After reading this method found Python source developers are too bull X, so the classic algorithm all want out.

"Pastry after Dinner"

This method is mainly used to remove the formulation characters at both ends according to specific rules. If the sentence3 is a good application.

For example: Intercept the numbers in a string, or get the string between the first and last occurrences of an attribute character, and so on.

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.