Various small details of Python's String.strip (s[, chars]) method

Source: Internet
Author: User

The following English instructions are given by the official:

String.strip (s[, chars])

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

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

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 ') + ' * '


Python's String.strip (s[, chars]) method of various small details

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.