string of Python

Source: Internet
Author: User

String Common methods

S.find () S.join () S.split () S.replace () S.strip ()

S. isalnum()/s.isalpha()/s.isdigit( )

s.upper ()/ s.lower()

S.count ()

Len (s)

1. sequence type common operations

a) indexes and slices

Index: Example:s[0] left-to- right: Index starting from 0 ; right-to-left: Index starting from 1

slices: Example:s[0:2]

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/90/wKiom1mB32jCxGVbAABMX3KbE74532.png "style=" float : none; "title=" Image 1.png "alt=" Wkiom1mb32jcxgvbaabmx3kbe74532.png "/>

Example :

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9D/90/wKioL1mB32nDr2CbAAAcVXV-Qyk778.png "style=" float : none; "title=" Image 2.png "alt=" Wkiol1mb32ndr2cbaaacvxv-qyk778.png "/>

Example :

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9D/90/wKioL1mB32mydxcOAAAwqyLhaIc622.png "style=" float : none; "title=" Image 3.png "alt=" Wkiol1mb32mydxcoaaawqylhaic622.png "/>


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/90/wKiom1mB32nggJiJAAASFURh9F4153.png "style=" float : none; "title=" Image 4.png "alt=" Wkiom1mb32nggjijaaasfurh9f4153.png "/>

Extended slices : Specify step size , Example: S[0:9:2]

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/90/wKioL1mB32qwI0gPAAAjP-WPQcM882.png "style=" float : none; "title=" Image 5.png "alt=" Wkiol1mb32qwi0gpaaajp-wpqcm882.png "/>


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/90/wKiom1mB32qyzbYSAAAilmsnGp0824.png "style=" float : none; "title=" Image 6.png "alt=" Wkiom1mb32qyzbysaaailmsngp0824.png "/>

b)FormattingOutput: use python to output various types of string,integer,floating point number

Format:

Print "string %format1%format2... " % ( variable or value 1, variable or value 2 ...)

Symbols and their corresponding output formats:

%d Decimal integer

%f floating point number

%s String

%% for output symbol %

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/90/wKioL1mB32rBMdyDAAA1p_vVmmk986.png "style=" float : none; "title=" Image 7.png "alt=" Wkiol1mb32rbmdydaaa1p_vvmmk986.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/90/wKiom1mB32uCeXflAAAh_Qm14Lc425.png "style=" float : none; "title=" Image 8.png "alt=" Wkiom1mb32ucexflaaah_qm14lc425.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/90/wKioL1mB32vw5dznAAApbKzJURg037.png "style=" float : none; "title=" Image 9.png "alt=" Wkiol1mb32vw5dznaaapbkzjurg037.png "/>

c) forced type conversion

int(): Cast to integral type,

For example:

Note : Only will be by the number composition the string is converted to an integer type


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/90/wKiom1mB32yjxmFsAAAxQchfDb4766.png "style=" float : none; "title=" Image 10.png "alt=" Wkiom1mb32yjxmfsaaaxqchfdb4766.png "/>

Float(): Cast to float type for example

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/90/wKiom1mB32zAqXiMAAA2fRe-UeI466.png "style=" float : none; "title=" Image 11.png "alt=" Wkiom1mb32zaqximaaa2fre-uei466.png "/>

str (): Cast to a string such as :

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/90/wKioL1mB32yAPqboAAAqupr0aXI536.png "style=" float : none; "title=" Image 12.png "alt=" Wkiol1mb32yapqboaaaqupr0axi536.png "/>

d) Example of string conversion: Formatting the output menu list, converting the user's input value to the integer type and verifying

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/90/wKiom1mB322w6UB9AABBDMH8X38191.png "style=" float : none; "title=" Image 13.png "alt=" Wkiom1mb322w6ub9aabbdmh8x38191.png "/>

2. String Common methods

S.find (t): Find the substring T, find the first where it appears The index value is not found and returns -1

Example :

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/90/wKioL1mB323xdkMcAAA5X14MSfE933.png "style=" float : none; "title=" Image 14.png "alt=" Wkiol1mb323xdkmcaaa5x14msfe933.png "/>

S.split (Sep): Using Sep as the delimiter, the list returns the results.

Example:str= "www.baidu.com"

Str.split (".") get ["www", "Baidu", "com"] sequence

Example :

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/90/wKiom1mB326AB3zPAAAwWMX37qw227.png "style=" float : none; "title=" Image 15.png "alt=" Wkiom1mb326ab3zpaaawwmx37qw227.png "/>

S.join (ITER) uses S as a connector to concatenate all elements of the ITER For example:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/90/wKioL1mB327CFoE5AAAdR1tIk80588.png "style=" float : none; "title=" Image 16.png "alt=" Wkiol1mb327cfoe5aaadr1tik80588.png "/>

Example :

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/90/wKioL1mB326jxhaLAAAfq_irJhY183.png "style=" float : none; "title=" Image 17.png "alt=" Wkiol1mb326jxhalaaafq_irjhy183.png "/>

S.replace (old,new): replace

Example :

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9D/90/wKiom1mB33CRXZ1CAAAh4nAaD7o192.png "style=" float : none; "title=" Image 18.png "alt=" Wkiom1mb33crxz1caaah4naad7o192.png "/>


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9D/90/wKioL1mB33CRSqbAAAAvAXMMCEw476.png "style=" float : none; "title=" Image 19.png "alt=" Wkiol1mb33crsqbaaaavaxmmcew476.png "/>

S. Strip (t): Delete the start and end characters T ( remove whitespace characters without the parameter t )

Example: Remove white space character S1.strip ()

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/90/wKiom1mB33GD2vRwAAAuHch_0wY798.png "style=" float : none; "title=" Image 20.png "alt=" Wkiom1mb33gd2vrwaaauhch_0wy798.png "/>

S.isalpha ()/s.isdigit (): /Whether it is a letter/whether it is a number


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/90/wKiom1mB33HA8qICAAAuM6SLcU8827.png "style=" float : none; "title=" Image 21.png "alt=" Wkiom1mb33ha8qicaaaum6slcu8827.png "/>

The result is BOOL type

s.upper ()/ S.lower (): All letters converted to uppercase/lowercase letters,

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9D/90/wKioL1mB33Gj8dh3AAAimCoj1E8006.png "style=" float : none; "title=" Image 22.png "alt=" Wkiol1mb33gj8dh3aaaimcoj1e8006.png "/>

S.count (t) statistics on the number of occurrences of T

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9D/90/wKiom1mB4vKxWwaeAAAcBm983NM945.png "title=" s.png "alt=" Wkiom1mb4vkxwwaeaaacbm983nm945.png "/>

Instance

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9D/90/wKiom1mB33LwD0Q_AAA5xxy051E104.png "style=" float : none; "title=" Image 23.png "alt=" Wkiom1mb33lwd0q_aaa5xxy051e104.png "/>


This article is from the "Duyuheng" blog, make sure to keep this source http://duyuheng.blog.51cto.com/12879147/1953120

string of Python

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.