The way to python-Basics 5
1. Common string operations
# string operation name = "my name is kk" print (name.capitalize ()) # > > My name is kk#capitalize () # initials print (name.count ("a")) # > 1#count ("a") counts the number of aprint (name.center (50, "-")) # >-my name is kk-#center (50) "-") A total of 50 "-" are printed, with characters corresponding to name placed in the middle of print (name.endswith ("k")) # > True#endswith () ending # expandtabs (tabsize = 10), followed by 10 spaces name = "my name is kk" print (name.find ("name")) # > > 3#find () string can also be sliced names = "my name is {name} and {year}" print (names.format (name = 'kk') Year = 27)) # > > my name is kkand 27#format () format print (names.format_map ({'name':'kk','year':'27'})) # format_map () print (' ab123'.isalnum ()) # > True#isalnum () does this contain Arabic numerals You cannot use the special character print ('ab'.isalpha ()) # > > True#isalpha () to judge whether the English character print (' ab1'.isdecimal ()) # > False#isdecimal () is a decimal isdigit () is an integer print ('ab123'.isidentifier ()) # > > True#isidentifier () to determine whether it is a legal identifier print ("aaa" .islower ()) # > True#islower () Lowercase print ("33" .isnumeric ()) # > True#isnumeric () determines whether it is a number print ("My Name" .istitle ()) # the first letter is all uppercase # > Trueprint ("MY NAME" .isupper ()) # > True# is all uppercase print ("+" .join (['1'') ) # > 1. Print (name.ljust (50)) is often used in my name is kk**print (name.rjust (50)) '-')) # > >-- my name is kkprint ("AA" .lower () # > aaprint ("aa" .upper ()) # > > AAprint ("\ nAA") print ("\ nAA" .lstrip ()) # to the left space to wrap print ("AA\ n") print ("AA\ n" .rstrip ()) # to the right blank Print ("\ n AA\ n") print ("\ n AA\ n" .strip ()) # remove the left and right space newline character print ("geng". Replace ("g") "G", 1)) # replace The last 1 means replacing only the first print ("alex li" .rfind ("l")) # > finding the first l-corresponding subscript print ("1-2-3-4" .split ("+")) from right to left. Split ("+")) # > ['1','2','3','4']
2. the use of dictionaries
# use of dictionaries # Note: dictionaries are unordered Because there is no subscript info = {'s 1: "Xiao Li",'s 2: "Xiao Zhang",'s 3: "Xiaomi",} print (info) b = {'s 1: "kk", 22 kk 100, 1:1,} info.update (b) print (info) print (info.items ()) # result: {'s 1: 'Xiao Li,' s 2: Xiao Zhang Kk', 1: 1, S2: Xiao Zhang, S3: Xiaomi, 22: 100} dict_items ([('s 1, 'kk'), (1, 1), (' s 2, Xiao Zhang'), ('s 3, 'Xiaomi), (22) ) # print (info ['S1']) # info ["S1"] = "Little plum" # info ['s4'] = "kk" # print (info) # Delete # del info [' s3'] # info.pop ('s2') # Standard deletion pose # info.popitem () # any one of the # print (info) # > > {' s 4': 'kk' 'S1 plum'} # print (info.get ("S1")) # query whether this is in the dictionary # > Little plum # print ('s1' in info) # > True