python常用的数据类型包括整型(int)、字符串(str)、布尔值(bool)、列表(list)、元组(tuple)、字典(dict)、集合(set)
整型(int)
int操作方法
bit_length()就是查看十进制数转换成二进制在内存中占用了多少长度
1 a = 5
2 print(a.bit_length()) #当十进制用二进制表示时,最少使用的位数 3
int、bool、str三种转换
>>> i = 10 # int --> str
>>>str(i)10
>>> ii = -1
>>>str(ii)-1
>>> iii =0>>>str(iii)
>>> iiii = 000
>>>str(iiii)
1 >>> s1 = 20 # str --> int
2 >>>int(s1)3 20
4 >>> s2 = 500
5 >>>int(s2)6 500
7 >>> s3 = -122
8 >>>int(s3)9 -122
10 >>>
1 >>> i = 5 # int --> bool
2 >>>bool(i)3 True4 >>> a = -1
5 >>>bool(a)6 True7 >>> b =08 >>>bool(b)9 False
>>> int(True) #bool --> int
1
>>>int(False)
0>>>
>>> s4 = hello #str --> bool
>>>bool(s4)
True>>> s5 =
>>>bool(s5)
True>>> s6 = \
>>>bool(s6)
False
1 >>>str(True) # bool --> str2 True
3 >>>str(False)4 False
字符串(str)
python中凡是使用引号引起来的数据可以称为字符串类型,组成字符串的每个元素称为字符。
组成字符串的字符元素从左到右依次排列,是有顺序的(从0开始)。对字符串进行索引,切片出来的数据都是字符串类型。
切片操作
1 >>> s = python学习课程
2 >>> s1 =s[0]3 >>>s14 p
5 >>>type(s1)6
7 >>> s2 = s[6]8 >>>s29 学
10 >>> s3 = s[-1]11 >>>s312 程
13 >>> s4 = s[0:6]14 >>>s415 python
16 >>> s5 = s[:6]17 >>>s518 python
19 >>> s6 = s[6:]20 >>>s621 学习课程
22 >>> s7 = s[:8:2] #2 代表步长
23 >>>s724 pto学
25 >>> s8 = s[-1:-8]26 >>>s827 \
28 >>> s8 = s[-1:-8:-1]29 >>>s830 程课习学noh
31 >>> s9 =s[:]32 >>>s933 python学习课程
34 >>> s10 = s[-1:]35 >>>s1036 程
View Code
1 s = "123a4b5c"
2 s1 = s[:3]3 print(s1) #"123"
4 s2 = s[3:6]5 print(s2) #"a4b"
6 s3 = s[::2]7 print(s3) #"1345"
8 s4 = s[1:-2:2]9 print(s4) #"2ab"
10 s5 = s[-1]11 print(s5) #"c"
12 s6 = s[-3::-2]13 print(s6) #"ba2"
View Code
1 >>> s = "Turn right on this street."
2 >>> s1 = s.upper() #变大写
3 >>>s14 TURN RIGHT ON THIS STREET.
5 >>> s2 = s.lower() #变小写
6 >>>s27 urn right on this street.
8 >>> s.count("r")9 3
10 >>>
11 >>> s.count("r", 0, 9)12 2
13 >>> ss = "ascaljcdashc;LS"
14 >>>ss15 ascaljcdashc;LS
16 >>> ss.startswith("as") #以...开头
17 True18 >>> ss.startswith("asd")19 False20 >>> ss.startswith("sw", 5, -1) #加了切片
21 False22 >>> ss.startswith("jc", 5 , -1)23 True24 >>> ss.endswith("S") #判断是否以...结尾
25 True26 >>> ss.endswith("c", 6, 11)27 False28 >>> ss.endswith("c", 6, 12)29 True30 >>> s = "Hope has always been like a teenager, clean and pure peace of mind"
31 >>>s.split()32 [Hope, has, always, een, like, a, eenager,, clean, and, pure, peace, of, mind]33
34 >>> s.split(",")35 [Hope has always been like a teenager, clean and pure peace of mind]36
37 >>> s1 = "name:alex:age:18:gender:male"
38 >>> s1.split(":")39 [ ame, alex, age, 18, gender, male]40 >>> s1.split(":",2)41 [ ame, alex, age:18:gender:male]42 >>> s2 = ":tom:ethan:alex"
43 >>> s2.split(":")44 [ , om, ethan, alex]45 >>> s3 = "alex"
46 >>> "+".join(s3)47 a+l+e+x
48 >>> type("+".join(s3))49
50 >>> s4 = "name"
51 >>> " ".join(s3, s4)52 Traceback (most recent call last):53 File "", line 1, in
54 TypeError: join() takes exactly one argument (2given)55 >>> ss =s.split()56 >>>ss57 [Hope, has, always, een, like, a, eenager,, clean, and, pure, peace, of, mind]58 >>> " ".join(ss)59 Hope has always been like a teenager, clean and pure peace of mind
60
61 >>> li = ["s", "a", "b", "f", 33]62 >>>type(li)63
64 >>> "".join(li)65 Traceback (most recent call last):66 File "", line 1, in
67 TypeError: sequence item 4: expected str instance, int found68 >>> s = "Hope has always been like a teenager, clean and pure peace of mind"
69 >>>s.split()70 [Hope, has, always, een, like, a, eenager,, clean, and, pure, peace, of, mind]71 >>>
72 >>> s.split(",")73 [Hope has always been like a teenager, clean and pure peace of mind]74
75 >>> s1 = "name:alex:age:18:gender:male"
76 >>> s1.split(":")77 [ ame, alex, age, 18, gender, male]78 >>> s1.split(":",2)79 [ ame, alex, age:18:gender:male]80 >>> s2 = ":tom:ethan:alex"
81 >>> s2.split(":")82 [ , om, ethan, alex]83
84 >>> s3 = "alex"
85 >>> "+".join(s3)86 a+l+e+x
87 >>> type("+".join(s3))88
89 >>> s4 = "name"
90 >>> " ".join(s3, s4)91 Traceback (most recent call last):92 File "", line 1, in
93 TypeError: join() takes exactly one argument (2given)94 >>> ss =s.split()95 >>>ss96 [Hope, has, always, een, like, a, eenager,, clean, and, pure, peace, of, mind]97 >>> " ".join(ss)98 Hope has always been like a teenager, clean and pure peace of mind
99
100 >>> li = ["s", "a", "b", "f", 33]101 >>>type(li)102
103 >>> "".join(li)104 Traceback (most recent call last):105 File "", line 1, in
106 TypeError: sequence item 4: expected str instance, int found107 >>> s= "hello"
108 >>> s.strip() #去除空格
109 hello
110 >>> s5 = "as你好jhd"
111 >>> s5.strip("ajhsd")112 你好
113 >>> s5.strip("ajhs")114 你好jhd
115 >>> s6 = "as你r好jhd"
116 >>> s6.strip("ajhsd")117 你r好
118 >>>
119 >>> "**barry*****".strip()120 **barry*****
121 >>> "**barry****".strip("*")122 arry
123 >>> "**barry****".lstrip("*")124 arry****
125 >>> "**barry****".rstrip("*")126 **barry
127
128 >>> s = "他叫王麻子,今年25,目前就读于双一流高校,王麻子喜欢唱歌,班上的人都叫王麻子歌神"
129 >>> s.replace("王麻子","张三")130 他叫张三,今年25,目前就读于双一流高校,张三喜欢唱歌,班上的人都叫张三歌神
131 >>> s.replace("王麻子","张三",2)132 他叫张三,今年25,目前就读于双一流高校,张三喜欢唱歌,班上的人都叫王麻子歌神
133 >>> s.replace("王麻子","张三",1,,3)134 File "", line 1
135 s.replace("王麻子","张三",1,,3)136 ^
137 SyntaxError: invalid character inidentifier138 >>> s.find("双一流") #返回的找到的元素的索引,如果找不到返回-1
139 16
140 >>> s.find("25", 1, 10)141 8
142 >>> s.find("哈哈")143 -1
如果觉得《python三种基本数据类型_python基础数据类型》对你有帮助,请点赞、收藏,并留下你的观点哦!