Python sequence II and Python sequence II

Source: Internet
Author: User

Python sequence II and Python sequence II

  • String

All standard sequence operations also apply to strings, but the strings are immutable.

String formatting:

# Example> 'num is: % d' % 18 'num is: 18 '>>> 'str is % s' % 'string' 'str is string # The width and accuracy of the field and the role of' * '>>>' % 10f' % math. pi # width: 10' 3.141593 '>>>' %. 10f' % math. pi # precision 10' 3. 1415926536 '>>>' % 10.2f '% math. pi # width 10 precision 3.14 '>>>' %. * F' % (5, math. pi) '3. 14159 '>>>' % * F' % (5, math. pi) '3. before the 141593 '# character width and precision value, you can place a' sign '>' % 010.2f '% math. pi # The value 0 indicates that '123' will be filled with 0. 14' >>> '%-10.2f' % math. pi # '-' indicates the left alignment '3. 14'> '% + 10f' % math. pi '+' indicates that + + + 3.141593 is displayed before a positive number'

String method (built-in function)

# Find (str, beg = 0 end = len (string) checks whether str is contained in a string. If the specified range is beg and end, checks whether it is included in the specified range, if the returned index value is included, otherwise-1 >>> str = 'taking off tight shoes '>>> str. find ("off") 7 >>> str. find ("off",)-1 # join (seq) uses the specified string as the separator to represent all elements in seq) merged into a new string >>> s1 = '/' >>> seq = ['', 'user', 'jm ', 'desktop '] >>> s1.join (seq)'/User/jm/Desktop '# all uppercase characters in the lower () Conversion string are lowercase. >>> s1 = 'hello, WORLD! '>>> S1.lower ()' hello, world! '>>> S1.title () # uppercase letters 'hello, World! '# Replace (old, new [, count]) replaces str1 with str2. If count is specified, it cannot exceed count. >>> S2 = 'fake-smile jail cells '> s2.replace ('l', 'xxx') 'fake-smixxxe jaixxx cexxxxxxs'> s2.replace ('l ', 'xxx', 2) 'fake-smixxxe jaixxx cells '# split (str = "", num = string. count (str) num = string. count (str) uses str as the separator to intercept a string. If num has a specified value, only the num substring >>>> s3 = 'user/jm/Desktop '>>> s3.split ('/') ['user', 'jm ', 'desktop '] # strip ([chars]) on the string, execute lstrip () and rstrip () (remove the character specified at the beginning and end of the string) >>> s4 = '*** Hello, *** world ***'> S4.strip ('*') 'Hello, *** world' # character of the string converted by the translate () method based on the table (containing 256 characters) given by the parameter table, put the characters to be filtered out in the deletechars parameter. Table = str. maketrans ('cd', 'ef ') # The maketrans () method is used to create a conversion table for character ing >>> s5 = 'abcdefghijklmnopqrstuvwxy' >>> s5.translate (table) 'abefefghijklmnopqrstuvwxy' # Another >>> bytes_tabtrans = bytes. maketrans (B 'abcdefghijklmnopqrstuvwxyz', B 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')> s6.translate (bytes_tabtrans, B 't') B 'HP: // WWW. CNBLOGS. COM/TABBY /'

 

  • Dictionary

Basic knowledge

# Create and use a dictionary> dic = {'jameson': '123456', 'jack': '123456', 'abel ': '000000'} # create # Use dict () to create a dictionary> arr = [('name', 'jack'), ('num', 1003)]> dic2 = dict (arr) >>> dic2 {'num': 1001, 'name': 'jack'} # Or >>> dic3 = dict (name = 'jack ', num = '000000') >>> dic3 {'num': '000000', 'name ': 'jack'} # Use >>> dic ['abel '] '000000' >>> "jack's num is % (Jack) s. "% dic" Jack's num is 1002. ">>> dic [" Jack "] = 1005 # modify> dic [" Berg "] = 1007 # Add> dic {'abel ': '123 ', 'jack': 1005, 'jameson': '123', 'berg ': 1001}

 

Dictionary Method

# Clear () delete all elements in the dictionary >>> dic2 = {'num': 1001, 'name': 'jack' }>>> dic2.clear () >>> dic2 {}# copy () copy dictionary >>> dic4 = dic # This can also >>> dic4 {'abel ': '123', 'jack': 1003, 'jameson': '000000', 'berg ': 1001 }>>> dic5 = dic. copy () >>> dic5 {'abel ': '123', 'jack': 1003, 'jameson': '123456', 'berg ': 1007 }>>> dic ["Jack"] = 1009 # try to modify the dic value >>>> dic4 {'abel ': '123', 'jack': 1003, 'jameson': '000000', 'ber': 1001} # shortest copy >>> dic5 {'abel ': '123', 'jack': 1003, 'jameson': '123456', 'berg': 1005} # Deep copy # fromkeys (seq [, value]) create a new dictionary and use the elements in sequence seq as the dictionary key. val is the initial value corresponding to all the keys in the dictionary >>> dict. fromkeys (['name', 'age']) {'age': None, 'name': None }>>> dict. fromkeys (['name', 'age'], 'unknown ') {'age': 'unknown', 'name': 'unknown '} # get (key, if the value is not in the dictionary, the default value is returned. # In addition, check whether a dictionary has a key that uses in> 'jack' in dicTrue> 'name' in dicFalse # items () to return a list that can be traversed (key, value) tuples> dic. items () dict_items ([('abel ', '123'), ('jack', 1003), ('jameson', '123'), ('berg ', 1007)]) # keys () returns all keys of a dictionary in the List> dic. keys () dict_keys (['abel ', 'jack', 'jameson', 'berg']) # pop gets the value of the given key, then remove it from the dictionary (del, remove, pop, hah)> dic. pop ("Jack") 1009 >>> dic {'abel ': '000000', 'jameson': '000000', 'berg': 1003} # popitem () returns and deletes a random key and value in the dictionary. >>> dic. popitem () ('abel ', '000000') >>> dic {'jameson': '000000', 'berg': 1003} # setdefault (key, default = None) similar to get (), but if the key does not exist in the dictionary, a key is added and the value is set to default >>> dic. setdefault ("Carl") >>> dic {'Carl ': None, 'jameson': '000000', 'berg': 1001} # values () return all values in the dictionary in a list> dic. values () dict_values ([None, '000000', 1001]) # update (dic2) update the dictionary dict2 key/value pairs to dict> dic6 = {'name': 'runoob', 'age ': 7 }>>> dic7 = {'sex ': 'female' >>>> dic6.update (dic7) >>>> dic6 {'sex ': 'female', 'name ': 'runoob', 'age': 7}

 

NOTE: Refer to basic Python tutorial (version 2nd)-Revision

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.