Python String functions

Source: Internet
Author: User

1, First letter capital

' GHOSTWU '>>> s.capitalize ()'ghostwu'

2,replace, replacing

>>> s ='My name is GHOSTWU'>>>s'My name is GHOSTWU'>>> S.replace (' -',' -' )'My name is GHOSTWU'>>>s'My name is GHOSTWU'>>>

Check Help

>>> Help (Str.replace)

If it is a process-oriented function usage, direct help (the function name), such as Help (ABS)

Usage notes:

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 is replaced.

Accept 3 parameters, the first character to be replaced, the second with what character to replace, the number of times the third substitution, if not passed, default all replace

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: Cutting

1>>> ip='127.0.0.1'2>>>IP3 '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']Ten>>> Ip.split ('.', 3 ) One['127','0','0','1'] A>>>

4, using the string module, use the following:

1 Import string 2 >>> Help (string.capitalize)34'ghostwu' 5 >>> string.capitalize (s)6'ghostwu'
1>>>Importstring2>>> s ='My name is GHOSTWU'3>>> String.Replace (s),' -',' -' )4 'My name is GHOSTWU'5>>>IP6 '127.0.0.1'7>>> string.split (IP,'.' )8['127','0','0','1']9>>> string.split (IP,'.', 1 )Ten['127','0.0.1'] One>>>

5,filter: Filter, use this function to filter out an even number of a sequence

1>>>defIsEven (n):2...ifn% 2 = =0:3...returnTrue4 ... 5>>> IsEven (10 )6 True7>>> IsEven (3 )8>>> Range (10 )9[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]Ten>>> L = Range (10 ) One>>>Filter (IsEven, L) A[0, 2, 4, 6, 8] ->>>

Python String functions

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.