1. String manipulation
The string can be evaluated by subscript, but because the string is an immutable variable, the value cannot be modified by subscript.
str = ' Lily Terry Mark '
Name[0] #获取第1个字符串值
Name[0:7] #获取第1 The string value of the ~
Name[:7] #缺省开始位置, the default value starts from the 1th element
Name[0:] #缺省结束位置, default to end of String end
The string contains the judgment operator: in, not in, is
' Lily ' in Names
' Lily ' not in Names
' Lily ' is ' lily ' is judging if the same memory address
A For loop inside python that loops through every element of an object
For name in Names:
Print (' name: ', name)
String Other operations
s = ' Hello World '
S.capitalize () # Capitalize first letter
S.center (50, ' _ ')) # Total length 50, put s in the middle
S.endswith (' u ') # does it end with X
S.expandtabs (30) # Number of times to fill \ t
S.find (' World ', 0,15) # Find the index of the string, find the index that returns the first occurrence, cannot find the return-1
S.rfind (' wo ') # reverse lookup, returns the subscript of the rightmost character
Formatting of strings
S.format (s= ' Lily ', age=18)) # format characters
S.format_map ({' s ': ' Lily ', ' age ': 19})
string format judgment
' AbA123 '. Isalnum () # contains numbers and letters
' AbA '. Isalpha () # is the English alphabet
' 122 '. IsDigit () # is a number
' AA '. Isidentifier () # is a valid variable name
' AA '. Islower () # Whether it is a lowercase letter
String case
' AA '. Isupper () # is an uppercase letter
S.lower () # Turns lowercase
S.upper () # becomes uppercase
' Abcde '. Swapcase () # case reversal
String to go to space
' \nmysql \ n '. Lstrip () # By default, the left space and line breaks are removed
' \nmysql \ n '. Rstrip () # Recognize and remove the right space and line breaks
' \nmysql \ n '. Strip () # Default to remove both spaces and line breaks
' MySQL '. Strip (' m ') # When a string is passed in, only the specified string is removed
String mappings
p = Str.maketrans (' abcde ', ' 12345 ') #前面的字符串和后面的字符串映射
Print (' Ccab '. Translate (p)) #输出按照上面maketrans做映射后的字符串
String substitution
' MySQL is db. MySQL '. replace (' MySQL ', ' Oracle ') #替换字符串
String cutting
' 1+2+3+4 '. Split (' + ') # cuts A string, returns a list, separates the string by the specified string, and then places it in a list
# If you don't write anything, separated by a space, multiple spaces are counted as a
' 1+2\n1+2 '. Splitlines () # Split by line break
String connection
Print (' + '. Join ([' hhe ', ' haha ', ' ee ')) # The Join method is used to stitch up each element of an iterative object through a string
String combinations
Import string
Print (string.ascii_letters+string.digits) # Case Letter + number
Print (string.ascii_lowercase) # lowercase Letter
Print (string.ascii_uppercase) # Uppercase
Print (string.digits) # all numbers
Print (string.hexdigits) #所有数字 + uppercase and lowercase letters
Built-in functions for working with strings
Len (str) #串长度
CMP ("Myfriend", str) #字符串比较. First large, returns 1
Max (' abcxyz ') #寻找字符串中最大的字符
Min (' abcxyz ') #寻找字符串中最小的字符
String conversions
Float (str) #变成浮点数, float ("1e-1") results are 0.1
int (str) #变成整型, int ("12") result is 12
Int (str,base) #变成base进制整型数, int ("11", 2) result is 2
Long (str) #变成长整型,
Long (str,base) #变成base进制长整型,
2. List operation
Create a list
Names =[1,2,3, "Lily", "Mark" # Definition List
List operations
Print (names[3]) # The 4th list element value is taken by subscript
Print (names[-1]) # subscript is-1, which means the last element is taken.
Names[0]= ' Terry ' # modifies the first list element value
List element Additions
L.append (VAX) # Adds a value to the list from the end
L.insert (Index,vax) #在指定的索引处添加元素
List element deletion
L.pop (index) #缺省index返回最后一个元素 and removed from list without deleting the element at the specified position by default
L.remove (Var) #删除指定的值, and pop method to distinguish, pop if deleted, the pass is subscript, remove is a value of an element
DELL[1] #删除指定下标的元素
Dell[1:3] #删除指定下标范围的元素
List Element Lookup
L.index (var,start,stop) #返回该元素的索引, start to stop specifies the range, returns the first if there is more than one, returns an exception
L.count (Var) #该元素在列表中出现的个数, not found for 0
List sort
L.sort () #排序
L.reverse () #倒序
List element Merging
L1+L2 #列表操作符 "+", two lists merged
L.extend (list) #把另一个列表每个值逐一添加到前面的列表中 list empty l.clear ()
Loop List
For name in Names:
Print (name)
Multidimensional arrays
List = [1,2,3,4,5, ' Lily ', 23.3,[' mark ', ' Lucy ', [' name ', ' sex ']]
3. Dictionary operation
Create a dictionary
DiC = {' stu1 ': ' Mark ', ' stu2 ': ' Lucy ', ' stu3 ': ' Lilei '}
Dictionary additions or modifications
Dic[key] = value
Dictionary Delete
Dic.pop (key) # standard method of deletion
del Dic[key] # using the Del method to delete
Dic.popitem () # randomly deletes a value
Dictionary Lookup
Dic.get (Key) # Gets the value of key for, which returns none if key does not exist
Dic[' Marry ' # gets the value of key for this method if key does not exist, it will be an error
' Marry ' in dic # Determines whether marry is in the dictionary, returns TRUE or False
Dictionary built-in methods
Dic.values () #以列表的形式返回字典中的值, the list of return values can contain repeating elements
Dic.keys () #返回字典键的列表
Dic.setdefault (Key, Value) # If this key exists, then do not move it, do not exist, add a
Dic.update (DIC2) # Updates the dictionary value, if key exists, it is updated, does not exist to add
Dic.items () # Dictionary converted to a list
Dictionary loops
DiC = {' stu1 ': ' Mark ', ' stu2 ': ' Lucy ', ' stu3 ': ' Lilei '}
For K in DiC:
Print (K,dic[k]) #打印key和value的值, recommended this way, faster
4, tuple operation
A python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuple= (' A ', ' B ', ' C ', ' d ', ' e ')
tuple = (' a ',) #元组中只包含一个元素时, you need to add a comma after the element
accessing tuples
Tuple[key]
modifying tuples
element values in tuples are not allowed to be modified, but they can be combined in groups of tuples
Tuple1+tuple2
Delete a tuple
element values in tuples are not allowed to be deleted, but you can use the DEL statement to delete an entire tuple
Del tuple
Tuple built-in functions
CMP (TUPLE1,TUPLE2) # Comparison of two elements
Len (tuple) #计算元组元素个数
Max (tuple) #返回元组中最大值
Min (tuple) #返回元组中最小值
Tuple (seq) #将列表转换为元组
Python list, dictionary, string Common operations