Python's method of removing a hollow lattice of strings

Source: Internet
Author: User
We often encounter a lot of whitespace in the processing of the problem, a one to remove the manual is not what we programmers should do, today this tip of the article script home to tell you how to use Python to remove the space in the string.
Let's first create a string variable s with n spaces around it, see the code:
Copy the Code code as follows:

>>> s = "Home of the script"
>>>


Remove the string space, in Python has its built-in method, do not need us to build the wheel.
Lstrip: Delete the left space
This string method removes the space before the start position of the string s.
Copy the Code code as follows:

>>> S.lstrip ()
' Home of the script '


Rstrip: Delete right-connected spaces
This built-in method can delete all the spaces at the end of the string and see the following demo code:
Copy the Code code as follows:

>>> S.rstrip ()
' Home of the script '


Strip: Remove spaces at both ends
Sometimes we read the contents of the file, each line of 2 sides have a space, can be removed all at once, the word character has a built-in strip () method can do.
Copy the Code code as follows:

>>> s = "Home of the script"
>>> S.strip ()
' Home of the script '


Script House Tip: You can use the Dir (str) method to get a list of all methods of STR strings.

  • 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.