Python learning path 3: python learning path

Source: Internet
Author: User

Python learning path 3: python learning path
Content of this section:

  • Common string processing.
  • Format the output string.
  • String replacement.
  • Conversion of strings and binary data.
  • Dictionary operations
String operation

Common string processing

Name = 'vector 'print (name. capitalize () # print (name. count ('E') # determine the number of print (name. center (50, '-') # print 50 characters in total, place the vector in the middle, and fill print (name. endswith ('R') # judge the end of a string. If it ends with r, trueprint (name. expandtabs (tabsize = 10) # If \ t is added to the string, add the tabsize space print (name. find ('EC') # locate the position of e in the string print (name. isalnum () # returns Trueprint (name. isalpha () # returns Trueprint (name. isdecimal () # If the string is a decimal number, Trueprint (name. isdigit () # returns Trueprint (name. isidentifier () # returns Trueprint (name. islower () # determines whether it is in lowercase. If yes, Trueprint (name. isupper () # determines whether it is in upper case. If yes, Trueprint (name. isspace () # determines whether it Is a space. If yes, Trueprint ('My Name Is 'Is returned '. istitle () # determines whether it is the title (uppercase). If yes, Trueprint ('+' is returned '. join (['1', '2', '3']) # Splice each element in the list with '+' to print (name. ljust (50, "*") # If the string length is less than 50, use * to supplement print (name. must ust (50, "*") # If the string length is less than 50, use * to supplement print (name. lower () # converts uppercase to lowercase print (name. upper () # converts lowercase letters to uppercase print ('\ nvector '. lstrip () # Remove the space on the left of the string and press ENTER print ('vector \ n '. rstrip () # Remove the spaces on the right of the string and press ENTER print ('\ nvector \ n '. strip () # Remove the spaces on both sides of the string and press ENTER print ('name is a Book '. rfind ('A') # Find a from the right and return the position print ('name is a Book '. split ('') # splits the string into a list by space. If no parameter is specified, the default Delimiter is space print ('name is \ na Book '. splitlines () # load the string into a list print ('name is a Book' Based on the linefeed '. swapcase () # case-insensitive print ('name is a Book '. title () # change the string to the title (uppercase) print ('name '. zfill (50) # If there are less than 50 strings, fill the string with 0 on the left.

Format outputString

Name = 'vector {name} {age} 'print (name. format (name = 'Rev ', age = 123) # format the string print (name. format_map ({'name': 'Rev ', 'age': 123}) # format a string in dictionary format

String replacement

# Use maketrans to set the replacement rule, and then use translate to replace p = str. maketrans ('asdfgn ', '000000') # The first parameter is the replaced character, and the second parameter is the replaced character print ('name '. translate (p) # input the rule p and print it out as 61me, because n and a are replaced by print ('name') by 6 and 1 '. replace ('n', 'B', 1) # replace n with B. The third parameter indicates the number of replicas. If n is not specified, all replicas are replaced by default.

Conversion of strings and binary data

Msg = 'I love the Yanbian daily empire' print (msg) # string output print (msg. encode (encoding = 'utf-8') # convert the string to binary print (msg. encode (encoding = 'utf-8 '). decode (encoding = 'utf-8') # convert binary data to a string
The encode parameter indicates the format before and the decode parameter indicates the format to convert. If encode and decode do not write parameters, the default value is UTF-8.

 

Dictionary

A dictionary is a key-value data type. It is used to query the details of a corresponding page through strokes and letters.

Dictionary features:

  • Dict is unordered
  • The key must be unique and so must be de-duplicated.

Syntax:

info = {    'str1':'zhangsan',    'str2':'lisi',    'str3':'wangwu',    'str4':'zhaoliu'}

 

Common dictionary operations:

Info = {'str1': 'hangsan', 'str2': 'lisi', 'str3': 'hangzhou', 'str4': 'zhaoliu'} print (info) # print all dictionaries print (info ['str2']) # print the dictionary element info ['str1'] = 'yangzirui 'based on the key name # The value will be modified, if the key name does not exist, del info ['str2'] # Delete the element info. pop ('str2') # This also deletes print (info. popitem () # delete a random key and return its key-Value Pair print (info ['str1']) # print the data. If the key name does not exist, an error is returned, print (info. get ('str2') # print data. If the key name does not exist, none is returned. print ('str1' in info) is recommended. # Check whether the key name exists and print true, otherwise print falseaaa = info. setdefault ('str5', 'liangsohohfefho') # If the key in info is str5, the corresponding value is directly returned. If no, then, the corresponding value a = {'str1': 'asdfsdfs',} info is returned. update (a) # merge dictionary a into info. If there is a cross, use the value info in Dictionary. items () # converts a dictionary to a list. Each element in the list is composed of tuples, and each key-value pair of the original dictionary forms a c = dict. fromkeys ([, 3], 'test') # create a new dictionary with a key of 1, 2, 3 and a value of test. If no value is written, the values are none; the values assigned later are shared by the three keys, so all the values are changed when you change them.

 

Loop dictionary:

Info = {'str1': 'hangsan ', 'str2': 'lisi', 'str3': 'hangww', 'str4 ': 'zhaoliu'} # first loop method for I in info: print (I, info [I]) # this should be the most basic loop # second loop method for k, v in info. items (): print (k, v) # The efficiency of this loop method is not as high as that of the first loop because the dictionary needs to be converted to a list.

Output result: (this is true for both cycles)

 

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.