Python basic learning notes (5)

Source: Internet
Author: User

 

Basic string operations

All standard sequence operations (indexing, partitioning, multiplication, determining membership, length, minimum and maximum values) are also applicable to strings, as described earlier. However, note that the strings are immutable.

 

String method:

String fromStringThere are many methods for "inheritance" in the module. Here we only introduce some particularly useful methods.

 

1,Find

FindYou can search for a substring in a long string. It returns the leftmost index of the substring location. If not found, return-1.

>>>'With a moo-moo here. And a moo-moo ther'. Find ('Moo')7 >>> Title ="Monty pyhon's flying circus">>> Title. Find ('Monty') 0>>> Title. Find ('Python')-1

2,Join

JoinMethod is a very important string method. It isSplitThe inverse method of the method, used to add elements to the queue:

>>> Seq = [ '  1  ' , '  2  ' , '  3  ' , '  4  ' , ' 5  '  ] >>> Sep = '  +  ' >>> Sep. Join (SEQ)  '  1 + 2 + 3 + 4 + 5  ' >>> Dirs = '' , '  USR  ' , '  Bin ' , '  Env  ' >>> '  /  '  . Join (dirs)  '  /Usr/bin/env  ' >>> Print   '  C:  ' + ' \\  '  . Join (dirs) c: \ USR \ bin \ env 

3,Lower

LowerReturns the lower-case encoding of the string.

If you want to write a "case-insensitive"CodeThen this method will be used.-----The Code ignores the case sensitivity.

 
>>>'Trondheim hammer dance'. Lower ()'Trondheim hammer dance'

4,Replace

ReplaceReturns the string after all the matching items of a string are replaced.

 
>>>'This is a test'. Replace ('Is','EEZ')'Theez EEZ a test'

If you have used the "search and replace" function in the text processing tool, you will not question the usefulness of this method.

 

5,Split

This is a very important method, and it isJoinIs used to split strings into sequences.

>>> '  1 + 2 + 3 + 4 + 5  ' . Split ( '  +  '  )[  '  1  ' , '  2  ' , ' 3  ' , '  4  ' , '  5  '  ] >>> '  /Usr/bin/env  ' . Split ( '  /  '  )[  '' ,'  USR  ' , '  Bin  ' , '  Env  '  ] >>> '  Using the default  '  . Split ()[  '  Using  ' ,'  The  ' , '  Default  ' ]

6,Strip

StripMethod returns a string that removes spaces on both sides (excluding internal spaces:

 
>>>'Internal white space is kept'. Strip ()'Internal white space is kept'

 

 

Dictionary

 

Dictionary usage

Fields in reality andPythonFields are constructed, so that you can easily find a specific word (key) and find its meaning (value ).

In some cases, dictionaries are more suitable than lists:

#Represents the status of the game board. Each key is a tuples composed of coordinate values;

#Number of file modifications stored, using the file name as the key;

#Digital phone number/Address book

 

Create a list of person names and four extension numbers:

>>> Names = [ '  Zhangsan ' , '  Lisi  ' , '  Wangwu  ' , '  Sunliu  '  ] >>> Numbers = [ '  2313  ' , '  9102 ' , '  3158  ' , '  4326  '  ]  #  Use the following method to query >>> Numbers [names. Index ( '  Zhangsan  '  )]  '  2313  ' 

 

Create and use dictionaries

You can create a dictionary as follows:

 
>>> Phonebook = {'Zhangsai':'2313','Lisi':'9102','Wangwu':'3158'}

A dictionary consists of multiple keys and their corresponding values. In the preceding example, the name is a key and the phone number is a value.

 

DictFunction

AvailableDictFunction, which is used to create a dictionary through sequence pairs such as other ing (such as other dictionaries) or (Key, value.

>>> Items = [( '  Name  ' , '  Gumby  ' ),( '  Age  ' , 42 )] >>> D = Dict (items) >>> D {  '  Age  ' : 42, '  Name  ' : '  Gumby  '  } >>> D [ '  Name  '  ]  '  Gumby ' 

DictThe function can also create a dictionary using the keyword parameter, as shown in the following example:

 
>>> D = dict (name ='Gumby', Age = 42)>>>D {'Age': 42,'Name':'Gumby'}

 

Dictionary example:

 #  Simple Database #  The name of a person is used as a key dictionary. Each person uses a different dictionary. The keys 'phone' and 'add' indicate their phone numbers and addresses respectively,  People = {  '  Zhangsan  '  :{  '  Phone  ' : '  2341  '  ,  ' ADDR  ' : '  Foo drive 23  '  },  '  Lisi  '  :{  '  Phone  ' : '  9102  '  , '  ADDR  ' : '  Bar Street 42  '  },  '  Wangwu  '  :{  '  Phone  ' : '  3158  ' ,  '  ADDR  ' : '  Baz Avenue 90  '  }}  #  Descriptive tags used for phone numbers and addresses are used for printing and output.  Labels = {  '  Phone  ' : ' Phone number  '  ,  '  ADDR  ' : '  Address  '  } Name = Raw_input ( '  Name:  '  )  #  Find the phone number or address? Use the correct key: Request = raw_input ('  Phone number (p) or address ()?  '  )  #  Use the correct key:  If Request = '  P  ' : Key = '  Phone  '  If Request = '  A ' : Key = '  ADDR  '  #  If the name is a valid key in the dictionary, the information is printed:  If Name In People: Print   "  % S's % s is % S.  " % (Name, labels [Key], people [name] [Key])
------------------------
# Input content >>> Name: zhangsanphone number (P) Or Address ()? P # Print results Zhangsan ' S phone number is 2341.

 

 

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.