Summary of usage of three functions for removing spaces in Python

Source: Internet
Author: User
Function: Strip () Lstrip () Rstrip ()

Function: Remove spaces in a string or specify characters

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

Note: The spaces here contain ' \ n ', ' \ R ', ' \ t ', '

Default Usage Instance

>>> Dodo = "Hello Boy" >>> dodo.strip () ' Hello Boy ' >>> dodo.lstrip () ' Hello Boy ' >>> do Do.rstrip () ' Hello Boy '

Second, remove the specified characters
Str.strip (' Do '): Removes characters specified at both ends of the string
Str.lstrip (' do '): Used to remove the character specified on the left
Str.rstrip (' do '): Used to remove the character specified on the right

Three functions can pass in a parameter ("Do" here, for example), specify the first and last characters to be removed, and the compiler will remove all corresponding characters from both ends until there are no matching characters

Note:
1. When you remove a specified character, you must not have a blank space when you pass in the parameter.
2. A combination of the specified character representation, such as ' do ' means ' dd ', ' do ', ' od ', ' oo ', ' ddd ', ' ooo ', etc.

Removing character instances

>>> Dodo = "Say hello say boy Saaayaaas" >>> dodo.strip (' say ') ' Hello say boy ' >>> dodo.strip (' Y As ') ' Hello say boy '

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

>>> Dodo.strip (' say ') ' Hello say Bo ' >>> dodo.lstrip (' say ') ' Hello say boy Saaayaaas ' >>> Dodo.rstrip (' say ') ' Say hello say boy '
  • 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.