Python string function, python string

Source: Internet
Author: User

Python string function, python string

1. uppercase letters

>>> s = 'ghostwu'>>> s.capitalize()'Ghostwu'

2, replace, replace

>>> s = 'my name is ghostwu, age is 20'>>> s'my name is ghostwu, age is 20'>>> s.replace( '20', '30' )'my name is ghostwu, age is 30'>>> s'my name is ghostwu, age is 20'>>> 

Query help

>>> help( str.replace )

For process-oriented function usage, direct help (function name), such as help (abs)

Usage instructions:

Replace (...)
S. replace (old, new [, count])-> string

Return a copy of string S with all occurrences of substring
Old replaced by new. If the optional argument count is
Given, only the first count occurrences are replaced.

Three parameters are accepted: the first character to be replaced, the second character to be replaced, and the third character to be replaced. If not passed, all parameters are replaced by default.

1 >>> str = '121212'2 >>> str.replace( '1', 'g' )3 'g2g2g2'4 >>> str.replace( '1', 'g', 1 )5 'g21212'6 >>> str.replace( '1', 'g', 2 )7 'g2g212'8 >>> str.replace( '1', 'g', 3 )9 'g2g2g2'

3. split: Cut

 1 >>> ip='127.0.0.1' 2 >>> ip 3 '127.0.0.1' 4 >>> ip.split( '.' ) 5 ['127', '0', '0', '1'] 6 >>> ip.split( '.', 1 ) 7 ['127', '0.0.1'] 8 >>> ip.split( '.', 2 ) 9 ['127', '0', '0.1']10 >>> ip.split( '.', 3 )11 ['127', '0', '0', '1']12 >>> 

4. Use the string module as follows:

1 >>> import string2 >>> help( string.capitalize )3 4 >>> s = 'ghostwu'5 >>> string.capitalize( s )6 'Ghostwu'
 1 >>> import string 2 >>> s = 'my name is ghostwu, age is 20' 3 >>> string.replace( s, '20', '30' ) 4 'my name is ghostwu, age is 30' 5 >>> ip 6 '127.0.0.1' 7 >>> string.split( ip, '.' ) 8 ['127', '0', '0', '1'] 9 >>> string.split( ip, '.', 1 )10 ['127', '0.0.1']11 >>> 

5. filter: filter. Use this function to filter out the even numbers of a sequence.

 1 >>> def isEven( n ): 2 ...     if n % 2 == 0: 3 ...             return True 4 ...  5 >>> isEven( 10 ) 6 True 7 >>> isEven( 3 ) 8 >>> range( 10 ) 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]10 >>> l = range( 10 )11 >>> filter( isEven, l )12 [0, 2, 4, 6, 8]13 >>> 

 

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.