The Strip,lstrip,rstrip of Python myths

Source: Internet
Author: User

Recently, when working with data, I want to remove the ")" symbol at the beginning of a string, so use Targetstr.lstrip (")") to find

When the processed data is inserted into the database, there will be an error encoding, so the online search for this post. The cause of the above coding error problem occurs

My understanding of the Lstrip function is wrong, the authoritative explanation is as follows:

Str.lstrip ([chars])

Return a copy of the string with leading characters removed. The chars argument is a string

Specifying the set of characters to be removed. If omitted or None, the chars argument

defaults to removing whitespace. The chars argument is not a prefix; Rather, all combinations

Of its values is stripped:

". Lstrip ()" www.example.com  '. Lstrip ('cmowz. ' )'example.com'

Strip, Lstrip, Rstrip, the situation is the same, the most important thing is to pay attention to:

The parameter chars is a collection, not prefix (all combinations of characters in the chars collection are deleted)

So I analyze the situation where I went wrong:

>>> A =")">>> B ="(">>>a'\xef\xbc\x89'>>>b'\xef\xbc\x88'>>>Len (a)3>>>A.lstrip (b)'\x89'>>>B.lstrip (a)'\x88'>>> Targetstr =") Ask Python master: (a)">>>PrintTargetstr.lstrip (a) Ask a Python expert: (a)>>> Print  targetstr.lstrip (b)? Ask a Python expert: (a) >>> Targetstr ="(1) Ask Python master: (a)">>> Print  targetstr.lstrip (a)? 1) Ask Python master: (a) >>>PrintTargetstr.lstrip (b)1) Ask Python master: (a)

So the solution I'm using is as follows,

Method 1:

>>> Targetstr =") Ask Python master: (a)">>> targetstr = targetstr[3:]ifTargetstr.find (")") = = 0ElseTargetstr>>>Printtargetstr for a Python master: (a)>>> Targetstr ="(1) Ask Python master: (a)">>> targetstr = targetstr[3:]ifTargetstr.find (")") = = 0ElseTargetstr>>>PrintTargetstr (1) Ask Python master: (a)

Method 2:

>>> Len (U")")1>>> targetstr = u") Ask Python master: (a)">>>Printtargetstr) Ask Python master: (a)>>>Targetstru'\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'>>> targetstr = Targetstr.lstrip (u")")>>>Targetstru'\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'>>>Printtargetstr for a Python master: (a)>>> targetstr = u"(1) Ask Python master: (a)">>>PrintTargetstr (1) Ask Python master: (a)>>>Targetstru'\uff081\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'>>> targetstr = Targetstr.lstrip (u")")>>>Targetstru'\uff081\uff09\u6c42\u6559python\u9ad8\u624b\uff1a\uff08\u4e00\uff09'>>>PrintTargetstr (1) Ask Python master: (a)

If you have a better way, be sure to tell me:)

In addition, from this post I also learned another point:

>>> c = Int ("\t22\n")>>> c22

References:
Ask a Python expert: a simple question, Lstrip function cutting error

The Strip,lstrip,rstrip of Python myths

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.