Python basic data type method use

Source: Internet
Author: User

One, str commonly used methods are: (In the Pycharm interface, in the location of the method by Ctrl + Left to view the details of the method of use information )

Len,count, Join,strip,isdigit,replace,title,startswith,endswith,expandtabs,format,find,index

Describe several of them as follows:

#占位符的使用a = "Qiu {0}, Xiao {1}" b = A.format (' ni '), print (b) #输出结果: ni,33 in----B automatically substituting {0},{1} in a string #qiu Ni,xiao 33#replace (" Part of the character in the string "," replaced by the new character ", replacement number of times) SS =" Hahahdeea "b = Ss.replace (" Ha ", BB", 2) #count ("One of the characters in the string")----result shows the number of characters in a string in the entire string #备注: The difference between find and index--find will not throw an exception if substring is not found, but will return -1,y so there will be no effect, and the index would be an error if it cannot be found.

Second, the list of commonly used methods mainly include:

Len,count,reverse,copy,append,remove,del,pop,update,clear

The difference between Remove,del and pop---is to remove elements from the list

A = [' A ', ' B ', ' CB ', ' dyh '] #remove---Delete the first qualifying element A.remove (' B ') print (a) #--------------------------------------------- -#pop---Delete characters by index, the return value can be assigned to other variables B = a.pop (1) #只有进行print了索引为1的元素才会从列表中移除print (b) #移除后的结果print (a) #--------------------- -------------------------#del---Delete characters by index, the return value cannot be assigned to other variables del (a[2]) print (a)

Three, the tuple commonly used methods mainly have (and list has a lot of similarities, except can not modify the value of the tuple (can not be deleted and modified)

Four, the dictionary commonly used methods mainly include:
Len,count,keys,values,items,clear,get,pop,update
d={' name ': ' Qiu ', ' age ': ' + '} #增d ={' name ': ' Qiu ', ' age ': '}d[' sex ' = ' man ' Print (d) #删 pop () "" through the specified key value, Remove a key value for the dictionary to return the deleted key corresponding to the value ' d={' name ': ' Qiu ', ' age ': '}print ' (D.pop (' name ')) print (d) #删 clear ()---emptying d={' name ': ' Qiu ', ' age ': '}d.clear ' () print (d) #字典操作之: Change d={' name ': ' Qiu ', ' age ': ' + '}d[' age '] = ' all ' Print (d) #查 get (), SetDefault (), Has_key () "Get () function: finds the corresponding value by the given key, and returns the None parameter if the given can be none in the Dictionary: Keysetdefault () function: Find the corresponding value by the given key, If the given can be none in the dictionary, return none, and add ' test ' to the dictionary: none key-value pair, parameter: Key,value ' d={' name ': ' Qiu ', ' age ': '}print ' (d.get (' name ')) Print (D.get (' Test ')) print (D.setdefault (' name ')) print (D.setdefault (' test_1 ')) print (D.setdefault (' test_2 ', 80)) Print (d) Print (D.has_key (' name ')) #字典操作之: Facelift items () "Yi the key value of the original dictionary to form a tuple, and finally returns ' d={' name ' in a list: ' Qiu ', ' age ': ' 18 ' }print (D.items ()) #字典操作之: Keys (), VALUES () "Returns all keys or values of the dictionary in the form of a list ' d={' name ': ' Tom ', ' age ': '}print ' (D.values ()) # Dictionary operation: Update () "D2 the key/value pairs of the dictionary to the d ' d={' name ': ' Qiu ', ' age ': '}d2 = {' Height ':", ' Tel ': 110}d.update (D2) print (d)





Python basic data type method use

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.