Python Str.strip () function

Source: Internet
Author: User

The following English instructions are given by the official:

String.strip (s[, chars])

Return a copy of the string with Leadingand trailing characters removed. If chars is omitted ornone, whitespace characters is removed. If given and not None, chars must is astring; The characters in the string would be stripped from the both ends of thestring this method was called on.

if chars is ignored or None, the white space string is removed. If the chars is not none and exists, the chars of the string's tail- ends will be removed.

Changed in version 2.2.3:the Charsparameter is added. The chars parameter cannot is passed in earlier 2.2versions.

The characters in the following example are tab -header and end with a space.

Line= ' Hello happybks! ‘

print ' * ' +line.strip () + ' * '

print ' * ' +line.strip (') + ' * '

print ' * ' +line.strip (') + ' * '

print ' * ' +line.strip (' h ') + ' * '

The output results are as follows:

*hello happybks!*

*hello happybks!*

*hello happybks!*

* Hello happybks! *

can be found to not pass parameters, will be the beginning and end of the string of the space,tab All Delete, the middle of the space and tab will not

If you pass a space or tab argument, the substring will still delete the beginning and end of the string, regardless of the space or tab.

When the passed argument is a different argument, the string at the beginning of the argument is not the string, then there is no effect

However, if the beginning and end of the string are other strings, and the passed arguments are the same string, the string at the beginning and end of the strings is cleared, regardless of the number of arguments. But case sensitive.

For example, the following example:

Line2= ' Haaaaahhaaaaaaahhhhh '

print ' * ' +line2.strip (' h ') + ' * '

Result output:

*aaaaahhaaaaaaahh*


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1812431

Python Str.strip () function

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.