A common operation in Python string
1.find: detecting a presence of a small return string is not contained in a large where the subscript string absence -1
Syntax: mystr.find (str) mystr = " colin "str =" Q "print ( mystr.find (str)) # -1 does not exist
2rfind: similar find is not right to left to find. Syntax: mystr.rfind (str)
3.index: index if there is no syntax error: mystr.index (str)
4.rindex: If the index does not exist on the error from right to left to find syntax: mystr.rindex (str)
5 .count: detection mystr number appears in str syntax: mystr.conut (str)
6.replace: replace, replace mystr in str1 into str2 syntax: mystr.replace (str1, str2, COUNT)
7.split: all you can split [Chinese, English, Chinese symbols, symbols] English
grammar: mystr.split ( ",")
8.startswith: If you start with what is not correct returns True returns False
syntax: mystr.startswith (str)
9.endswith : to what end if not properly return True and False
syntax: mystr.endswith (str)
10.strip: remove the spaces around the syntax: Print (mystr.strip ())
11.isalpha: determine whether the letters / characters or letters and Chinese characters combination
Syntax: print (mystr.isaplha ())
12.isdigit: determining whether a digital Syntax: print (mystr.isdigit ())
13.isalnum: Analyzing Chinese characters / letters / numbers or Chinese character / letter / number combination syntax: print (mystr.isalnum ())
Second, a common list of operations of
1.append: increase the element names = [ "Joe Smith", "John Doe", "Wang five", "Zhao six"]
Name.append ( "seven weeks" ) Print (names)
2.extend: a group of elements one by one can be placed into the list
names = [ "Joe Smith", "John Doe", "Wang Wu", "Zhao six"] names2 = [ "ha, doll "] names.extend (names2) Print (names)
3.Insert: insert element names in the specified location = [" Joe Smith "," John Doe "," Wang Wu "," Zhao six "] names.insert (0, 1) Print (names)
4. modify the element names [1] = "big baby" to find the corresponding index can be reassigned
5. find element in not in True If there was not present for the Flase
not If there was If there was True False
names = [ "Joe Smith", "John Doe", "Wang five", "Zhao six"]
the name = the iNPUT ( "Please enter the name you want to query: \ the n-")
IF name in names:
Print (" presence ")
the else:
Print (" no ")
6.index: Gets the element index values count in the list: the number of names to obtain elements that appear in the list = [ "Joe Smith", "John Doe", "Wang five", "Zhao six"]
the Name = "John Doe "Print (names.index (name))
Print (names.count (name))
7.del delete elements in accordance with the subscript names = [" Joe Smith "," John Doe "," Wang five "," Zhao six "] del names [2] print (names )
last element print 8.Pop deleted (names, POP ())
9.remove: delete names of elements according to the value = [ "Joe Smith", "John Doe", "Wang Wu "" Zhao six "] names.remove (" John Doe ") Print (names)
10.Reverse inversion and sort sorting
names = [" Joe Smith "," John Doe "," Wang Wu "," Zhao six " ]
Print (names.reverse ()) # reverse print (names.sort ()) # sort ascending
Print (names.sort (reverse = True) ) # sort descending
python list of common operations summary
Guess you like
Origin blog.csdn.net/beyongboy/article/details/104735895
Recommended
Ranking