一.字符串常用方法 下面是一些基本的字符串 操作函数(简称字串串的常用API),用于日常工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 str = "Clocky" str_proves = str .lower()print (str_proves) str = "Clocky" str_proves = str .upper()print (str_proves) str = "clocky is a good man" str_proves = str .capitalize()print (str_proves) str = "clocky" str_proves = str .title()print (str_proves) str = " clocky " str_proves = str .strip()print (str_proves) str = "clocky is?a good man" str_proves = str .split("?" )print (str_proves) str = "clocky is?a good& man" str_proves = str .split("?" )[1 ].split("&" )print (str_proves) str = ["Clocky" ,"is a good man" ] str_proves = " " .join(str )print (str_proves) str = "clocky is a good man" str_proves = str .replace("clocky" ,"Miko" )print (str_proves) str = "clocky is a good man" str_proves = str .find("a" )print (str_proves) str = "clocky is a good man" str_proves = str .startswith("clocky" )print (str_proves) str = "clocky is a good man" str_proves = str .endswith("man" )print (str_proves) str = "clocky7" str1 = "7" print (str .isdigit())print (str1.isdigit()) str = "clocky7" str1 = "c" print (str .isalpha())print (str1.isalpha()) str = "clocky7" str1 = "c" print (str .isalnum())print (str1.isalnum()) str = "clocky is a good man,whom is a student" str_proves = str .count("is" )print (str_proves) str = "clocky is {} good man,whom {} a student" num = "a" xiyu = "is" str_proves = str .format (num,xiyu)print (str_proves)
结果:
clocky
CLOCKY
Clocky is a good man
Clocky
clocky
['clocky is', 'a good man']
['a good', ' man']
Clocky is a good man
Miko is a good man
10
True
True
False
True
False
True
True
True
2
clocky is a good man,whom is a student
二.数字变量 2.1 进制 十进制直接表示;二进制0b表示; 八进制0o表示;十六进制0x表示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 num = 10 print (num) num = 0o10 print (num) num = 0x10 print (num) num = 0b10 print (num)
结果:
10 8 16 2
总结: 也就是说是多少进制就是一轮有多少个数,例如10进制09共10个数,八进制07共8个数,一轮所有数走完后就进1.
2.2 浮点数 实际上浮点数有多种不同的表示方法,最复杂的科学计数法是”假设Xe+y,X为原数,e+y则表示10的y次方,原式的结果就是将两者相乘”。 下面就是具体的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 num = 10. print (num) num1 = 10.0 print (num1) num2 = .9 print (num2) num3 = 1.52e-3 print (num3)
结果:
10.0
10.0
0.9
0.00152
2.3 布尔类型 · 布尔只有两个值:True、False
· 而数字中除了0其余皆为True
· 还有个需要注意的地方,空字符串为False,但空格字符串为True
· 用bool()将其转化为布尔值
1 2 print (bool (1 ))print (bool (0 ))
结果:
True
False
2.4 数字类型转换 ·bin()是将数字转换为二进制 ·oct()是将数字转换为八进制 ·hex()是将数字转换为十六进制 ·float()是将数字转换为浮点数 ·complex()是将数字转换为复数 ·int()是将数字转换为整数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 num = 6 print (bin (num)) num = 6 print (oct (num)) num = 6 print (hex (num)) num = 6 print (float (num)) num = 6 print (complex (num)) num = 6 num1 = 2 print (complex (num, num1)) num = 6.9 print (int (num))
结果:
0b110
0o6
0x6
6.0
(6+0j)
(6+2j)
6
2.5 四舍六入,五看齐,奇变偶不变(规则) 这里的意思是,在这种规则下,小数点后面的数,如果小于5,则舍弃,如果大于5,则进位。
如果等于5,那么看整数是奇是偶:
是奇数则进位;
是偶数则舍弃。
三.字节串 字节串通过在字符串前加上 $b$ 来表示,其整个数据类型为bytes。
1 2 3 a = b"hello" print (a)print (type (a))
结果:
b'hello'
<class 'bytes'>
四.空值 空值为None,其类型为NoneType(注:print()本体也为空值)。 举个简单的例子:
1 2 3 4 5 6 a = None print (a)print (type (a)) b = print ()print (b)
结果:
None
<class 'NoneType'>
None