The magic of the Strip () function you don't know in Python

Source: Internet
Author: User
"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

If name = = ' main ':    str = ' Hello world '    print ' [%s] '%str.strip ()

Method Two: Call the method in the string module

Import stringif name = = ' main ':    str = ' Hello world '    print ' [%s] '%string.strip (str)

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

Print Dir (string)

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

  Print dir (sys.modules[' builtin ')

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

Print Help (Builtins)

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:

If name = = ' main ':    str = ' Hello World '    print str.strip (' hello ')    print str.strip (' Hello '). Strip ()    Prin  T Str.strip (' Heldo '). Strip ()    #sentence 1    stt = ' h1h1h2h3h4h '    print stt.strip (' H1 ')                #sentence 2    s = ' 123459947855aaaadgat134f8sfewewrf7787789879879 '    print s.strip (' 0123456789 ')         #sentence 3

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:

# 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. ""    Return S.strip (chars)

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:

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

Specific code Execution process:

  Print Str.strip (' h ')    print str.strip (' h '). Strip (' E ')    print str.strip (' h '). Strip (' E '). Strip (' l ')    PR int 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 '). Strip ('    l ')    print str . Strip (' H '). Strip (' E '). Strip (' L '). Strip (' d '). Strip (' O '). Strip (' L '). Strip ()

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.

"Recommended"

1. Python Free video tutorial

2. Python strip () little-known traps

3. How to get started with Python basics how to use the Strip () function to go to a space \n\r\t

4. How to use Strip () and split () in Python

5. Detailed usage scenarios for strip functions in Python

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.