【python Language foundation 】 Difficult points sorting 1
6.1 character string
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
s
i
l
e
n
c
e
i
s
g
o
l
d
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
function
Example
value
Function description
len( character string )
len(‘Python Language ’)
8
Find the number of characters in the string
str( Numbers )
str(2.71828)
2.71828
Convert numbers to strings
chr( Encoding value )
chr(65)
A
Find the character corresponding to the encoded value
ord( character )
ord(“A”)
65
Find the code corresponding to the character
function
Function description
str1.find(subStr[,start[,end]])
Left to right search str1, return subStr stay str1 Index position of the first occurrence
str1.rfind(subStr[,start[,end]])
Right to left search str1, return subStr stay str1 Index position of the first occurrence
str1.index(subStr[,start[,end]])
Left to right search str1, return subStr stay str1 Index position of the first occurrence
str1.rindex(subStr[,start[,end]])
Right to left search str1, return subStr stay str1 Index position of the first occurrence
str1.count(subStr[,start[,end]])
Calculation subSt stay str1 Is the number of times
Method
Function description
str1.lower( )
The string str1 Convert to lowercase characters
str1.upper( )
The string str1 Convert to uppercase characters
str1.capitalize()
The string str1 title case , Other letters are lowercase
str1.title( )
The string str1 Capitalize each word in , Other letters are lowercase
str1.swapcase( )
The string str1 Case exchange of Chinese characters
Method
Function description
str1.split(sep=None,maxsplit=-1)
from str1 Start at left end , Use characters sep take str1 Split into multiple strings .maxsplit For the number of splits , The default value is -1.sep The default value is space .
str1.rsplit(sep=None,maxsplit=-1)
from str1 Start at the right end , Use characters sep take str1 Split into multiple strings .maxsplit For the number of splits , The default value is -1.sep The default value is space .
str1.join(iterable)
Connect multiple strings , And insert the specified character between adjacent strings .
Method
Function description
str1.strip(str2 )
Delete string str1 Blank characters at both ends or consecutive specified characters (str2 The characters in )
str1.rstrip(str2 )
Delete string str1 Trailing white space characters or consecutive specified characters (str2 The characters in )
str1.lstrip(str2 )
Delete string str1 A leading white space character or a continuous specified character (str2 The characters in )
Method
Function description
str1.startswith(str2 )
Judgment string str1 Whether to use the specified string str2 Start
str1.endswith(str2 )
Judgment string str1 Whether to use the specified string str2 end
>>> str1 = "Python: I like Python very much."
>>> str1
'Python: I like Python very much.'
>>> str1.replace("Python","Java")
'Java: I like Java very much.'
6.2 list
>>> numint = [1,3 ,6 ,8, 9] # Composed of integers of the same type
>>> nums = [5, 8, 0, -9, 3.14, -2.7] # Composed of different types
>>> words = ['apple','classroom','school','www'] # Composed of strings
>>> score = [[70,80,90], 240] # Some elements are lists
>>> student = ['05191234', ' Zhang San ', 18, ' male ', False, 530.5, " Electronic information "]
# Composed of different types
>>> list0 = [ ] # An empty list
>>> list1 = [2.7] # There is only one element list
>>> lst = ['I','love','you'] # List encapsulation
>>> [a,b,c] = lst # list lst Unsealing , Assign the elements in the list to a,b,c
>>> a ='I'
>>> b ='love'
usage
Function description
lst.append(item)
Put elements item Add to the end of the list
lst.insert(index, item)
Index in the list index Insert element before item
lst.extend(lst1)
In the list lst Tail insert list lst1 All elements of
lst.remove(item)
Remove the first element in the list item
lst.pop( )
Remove the last element in the list
del
Remove the index from the list index On the element
del
Remove entire list lst
lst[index] = item
Indexes index Replace the element on with the element item
>>> lst1 = [2,4,6,8]; lst2 = ['cat','dog']
>>> lst1.append(10)
>>> lst1=[2, 4, 6, 8, 10]
>>> lst2.insert(1,'goldfish')
>>> lst2=['cat', 'goldfish', 'dog']
>>> lst1.extend(lst2)
>>> lst1=[2, 4, 6, 8, 10, 'cat', 'goldfish', 'dog']
usage
Function description
len(lst)
Returns a list of lst The number of elements in
max(lst)
Returns a list of lst Maximum value of element in ( Elements must be of the same type )
min(lst)
Returns a list of lst The minimum value of the element in ( Elements must be of the same type )
sum(lst)
Returns a list of lst The sum of the elements in ( Element must be a number )
lst.count(item)
Returns a list of lst Medium element item Number of occurrences
lst.index(item)
Returns the element item In the list lst The first index in
lst.sort( )
Sort the list
lst.reverse( )
Sort the elements in the list in reverse order
sep.join(lst )
Use separator sep Put the string list lst It becomes a string
zip(lst1,lst2,…)
take lst1,lst2,… Elements in corresponding positions form tuples , Put it in zip In the object
enumerate(lst)
Enumeration list lst The elements in , Return enumeration object
6.3 Tuples
Tuples can be thought of as lists with fixed values , Access to tuples is similar to a list . The main difference between tuples and lists is : Tuples cannot be modified directly .