Python removes spaces at both ends of a space string

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 play snake nets to tell you, if you use Python to remove the space on both sides of the string.
Let's first create a string variable that has n spaces around it, see the code:

>>> s = "Iplaypython">>>

Remove the string space, in Python has its built-in method, do not need us to build the wheel.
Lstrip
This string method removes the space before the start position of the string s.

>>> S.lstrip ()"

Rstrip
This built-in method can delete all the spaces at the end of the string and see the following demo code:

>>> S.rstrip ()'  iplaypython'

Strip
Sometimes we read the contents of the file, there are spaces on both sides of each line, can be removed all at once, the word character has a built-in strip () method to do.

>>> s = "Iplaypython">>> s.strip ()'iplaypython'

Python removes spaces at both ends of a space string

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.