Python learning notes (3) strings

Source: Internet
Author: User
Some important methods of string

Find () returns the leftmost index of the substring location. If no index is found,-1 is returned.

Join () is the inverse method of the split () method, used to add elements to the queue.

 
SEQ = ['1', '2', '3'] Sep = '/' print Sep. join (SEQ) Sep = '\' print Sep. join (SEQ) running result:

Running result:

1/2/3

1 \ 2 \ 3

 

Lower (), returns the lower-case character of the string.

Replace (string a, string B) returns the string obtained after all the matches of string a are replaced with B.

Split () to split the string into a sequence.

Strip () removes strings on both sides (excluding internal.

Translate () and replace multiple single characters at the same time.

 

The following is a testProgram

 #-*-coding: UTF-8-*-'''created on 2013-7-7 @ Author: ginsmile ''' from string import maketransmystr = '! This is a simple .!! 'Mystr = mystr. Strip ('! ') # Remove spaces and exclamation points on both sides print mystr # This is a simple. print mystr. lower () # This is a simple. print mystr. capitalize () # This is a simple. print mystr. upper () # This is a simple. SEQ = mystr. split ('') print seq # ['this', 'is ', 'A', 'simple. '] print ''. join (SEQ) # This is a simple. table = maketrans ('cs ', 'kz') print Len (table) #256 print table [97: 123] # abkdefghijklmnopqrztuvwxyz # above, table is successfully written as a conversion table. Print 'this is another Simple. Cut the hair ~~ '. Translate (table) # thiz iz another zimple. Kut the hair ~~ 

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.