A summary of the use of three functions in Python to remove spaces _python

Source: Internet
Author: User
Tags in python

Function: Strip () Lstrip () Rstrip ()

Action: Removes spaces or specified characters from a string

One, default usage: Remove space
Str.strip (): Remove spaces on both sides of the string
Str.lstrip (): Remove the space on the left side of the string
Str.rstrip (): Remove the space to the right of the string

Note: The space here contains ' \ n ', ' \ R ', ' \ t ', '

Default Usage Instance

>>> Dodo = "Hello boy"

>>> dodo.strip ()
' Hello Boy '

>>> dodo.lstrip ()
' Hello boy '

>>> dodo.rstrip ()
' Hello Boy ' </span>

Ii. removal of specified characters
str.strip (' Do '): Remove characters specified at both ends of the string
Str.lstrip (' Do '): To remove the character specified on the left
Str.rstrip (' Do '): To remove the specified character on the right

Three functions can pass in an argument (here, for example, "do"), specifying the first and last characters to be removed, the compiler removes all the corresponding characters from both ends until no matching characters

Note:
1. Remove the specified characters can not appear at the end of the space, or incoming parameters need to add a space
2. A combination of specified character representations, such as ' do ' means ' dd ', ' do ', ' od ', ' oo ', ' ddd ', ' ooo ', etc.

Remove character instances

>>> Dodo = "Say hello say boy Saaayaaas"

>>> dodo.strip (' say ')
' hello say boy '
>>> ; Dodo.strip (' yas ')
' hello say boy '

#当传入的参数中加入空格时

>>> Dodo.strip (' say ')
' hello say bo '

>>> dodo.lstrip (' say ')
' hello say boy ' C10/>>>> Dodo.rstrip (' say ')
' Say hello say boy '

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.