Python check in on the fourth day, list, tuple
operation problem is not finished, continue tomorrow
List
The list is a basket, you can put everything in it. The elements can be of different types
list.append(obj)
Add a new object at the end of the list
list.extend(seq)
Append multiple values from another sequence at the end of the list at once
list.insert(index, obj)
The number index
insertion position obj
.
list.remove(obj)
Remove the first occurrence of a value in the list
list.pop([index=-1])
Remove an element from the list (the last element by default), and return the value of the element
Tuples and lists no difference, except that the tuple is created after can not be modified, so only count
and index
both methods. , Can also be spliced
Strings have some built-in operations
-
capitalize()
Convert the first character of the string to uppercase. -
lower()` Convert all uppercase characters in the string to lowercase.
-
upper()
Convert lowercase letters in the string to uppercase. -
swapcase()
Convert uppercase to lowercase and lowercase to uppercase in a string. -
count(str, beg= 0,end=len(string))
Returnsstr
the number of occurrences in string,beg
or ifend
specified, returnsstr
the number of occurrences in the specified range . -
endswith(suffix, beg=0, end=len(string))
检查字符串是否以指定子字符串
suffix结束,如果是,返回 True,否则返回 False。如果
beg和
end` If the specified value is specified, it will be checked within the specified range. -
startswith(substr, beg=0,end=len(string))
Check whether the specified string substringsubstr
beginning, and if so, returns True, otherwise False. If itbeg
andend
the specified value, it is checked in the specified range. -
find(str, beg=0, end=len(string))
Detectingstr
whether or not contained in the string, if the specified rangebeg
andend
, it is checked whether contained within the specified range, if included, return the index value starting, otherwise -1. -
rfind(str, beg=0,end=len(string))
Similarfind()
function, but start looking from the right. -
isnumeric()
If the string contains only numeric characters, it returns True, otherwise it returns False. -
ljust(width[, fillchar])
Return a new stringfillchar
with the original string left-justified and padded to the length with (default spaces)width
. -
rjust(width[, fillchar])
Return a new stringfillchar
with the original string right-aligned and padded to the length with (default spaces)width
.
【example】
str4 = '1101'
print(str4.ljust(8, '0')) # 11010000
print(str4.rjust(8, '0')) # 00001101
-
lstrip([chars])
Truncate the spaces or specified characters on the left side of the string. -
rstrip([chars])
Delete spaces or specified characters at the end of the string. -
strip([chars])
Performlstrip()
and on the stringrstrip()
. -
partition(sub)
Find the substring sub, divide the string into a triple(pre_sub,sub,fol_sub)
, and return if the string does not contain sub('原字符串','','')
. -
rpartition(sub)
Similar to thepartition()
method, but search from the right. -
replace(old, new [, max])
Replace the in the stringold
withnew
, ifmax
specified, the replacement will not exceedmax
times. format
String formatting symbol
symbol | description |
---|---|
%c | Formatting characters and their ASCII codes |
%s | Format the string, use the str() method to process the object |
%r | Format string, use rper() method to process object |
%d | Format integer |
%The | Format an unsigned octal number |
%x | Format an unsigned hexadecimal number |
%X | Format an unsigned hexadecimal number (uppercase) |
%f | Format floating point numbers, you can specify the precision after the decimal point |
%e | Format floating point numbers in scientific notation |
%E | Same function as %e, format floating point number in scientific notation |
%g | Decide to use %f or %e according to the value |
%G | The effect is the same as %g, according to the value of the decision to use %f or %E |
- Format operator auxiliary instructions
symbol | Features |
---|---|
m.n |
m is the minimum total width of the display, n is the number of digits after the decimal point (if available) |
- |
Used for left alignment |
+ |
Show plus sign (+) in front of positive numbers |
# |
Display zero ('0') in front of the octal number, display '0x' or '0X' in front of the hexadecimal number (depending on whether you are using'x' or'X') |
0 |
Fill the number displayed with '0' instead of the default space |