One, the string
Name ='Alex'#Capitalize first letterPrint(Name.capitalize ()) name='ALex Li 中文版 hahae'#First Letter LowercasePrint(Name.casefold ())#Center The specified length with = To be paddedPrint(Name.center (10,'='))#Develop scope statisticsPrint(Name.count ('e', 0, 5))#determines whether to end with the specified stringPrint(Name.endswith ('AE')) name='Alex\tli' #specify the number of spaces the TAB key occupiesPrint(Name.expandtabs (20))#specify the position of the string in the original stringPrint(Name.find ('Li'))#Formatting Assignmentsmsg ="My name is {0}, and I am {1} years old"MSG2="My name is {name}, and I am {years}"Print(Msg.format ('Alex', 22))Print(Msg2.format (age=22, name='Jack'))#is the English alphabetPrint('AD'. Isalpha ())#whether numbers and lettersPrint('A3D'. Isalnum ())#whether the decimalPrint('2'. Isdecimal ())#whether positive integerPrint('2'. IsDigit ())#is a valid variable namePrint('AL_EX'. Isidentifier ())#is lowercasePrint('AL_EX'. Islower ())#Uppercase or lowercasePrint('AL_EX'. Isupper ())#whether the numberPrint('3'. IsNumeric ())#whether it can be printedPrint('3'. Isprintable ())#whether the numberPrint('3'. IsNumeric ())#whether the first letter of each list is capitalizedPrint('My Name is Alex'. Istitle ())#concatenate all elements of a list into a string, acting in the opposite direction of splitPrint(','. Join (['Alex','Jack','Rain']))#Align LeftPrint('3'. Ljust (50,'-'))#Align RightPrint('3'. Rjust (50,'-'))#String LowercasePrint('Alex'. Lower ())#left to go spacePrint('Alex \ n'. Lstrip ())#go to the right and enter the blanksPrint('Alex \ n'. Rstrip ())#Create an escape rule to escape strings based on rulesFrom_str ='[Email protected]#$%^&'To_str='ABCDEFG'trans_table=Str.maketrans (to_str,from_str) str='Alex'str.translate (trans_table)#splits a string based on a specified stringPrint('Alex Li'. partition ('e'))#Replace string ContentsPrint('Alex Li'. replace ('L','L', 1))#splitting strings with newline charactersPrint("alex\n L\ni". Splitlines ())#The default is to split the string with a space separatorPrint("alex\n L\ni". Split ())#right-aligned, filled with 0Print('Alex Li'. Zfill (40))
Second, List
Lists can store more data than strings and find more convenient
names = ['Alex','Jack','Rain','Mack','Rain','Bob']#slicesPrint(Names[1:4:2])Print(names[-2:])Print(Names[:2])#Find IndexFirst_index = Names.index ("Rain")#to find the index of a second repeating value rainSecond_index = Names[first_index+1:].index ("Rain")Print(First_index, Second_index)#To delete a list elementdelNames[first_index+second_index+1]Print(names)#DeleteNames.pop (4)Print(names)#modifying list element valuesNAMES[4] ="Jason"Print(names)#List AdditionsNames + = [1, 2, 3, 4]Print(Names.extend ([1, 2, 3, 4]))Print(names)#nesting and taking values of listsNames2 = [['Alex', 22, ['Teacher', 3000]], ['Oldboy', 56]]Print(Names2)Print(names2[0][1]) names2[0][2][1] = 30000Print(Names2)
Three, meta-group
is a read-only list and cannot be modified. There are only two methods: Count, Index
Four, the dictionary
Features: unordered, key unique, high query efficiency
Names = { 'stu1101': {'name':'Alex',' Age': 22,'Hobbie':'Girl'}, 'stu1102':'Jack', 'stu1103':'Rain',}#SearchPrint(names['stu1101']['Hobbie'])#print (names[' stu1104 ')Print(Names.get ('stu1104','SDFSDF'))#Addnames['stu1104'] = ['Yangjian', 31,'DBA']#Updatenames['stu1104'][0] =' Wescker'Print(names)#DeletePrint(Names.pop ('stu1105','ssss'))delnames['stu1103']Print(names)
Python data structure Type: string, list, tuple, dictionary, collection