python基本数据类型包括:
1、整形int(不可变)、浮点数、布尔值、复数。
2、字符串str(不可变)、列表list(可变)、元组(不可变)。(有序,可用下标索引来访问,支持切片操作[0:5])
3、集合set(可变)(无序,没有索引,不能切片)
4、字典dict(可变)(无序)
序列:
包括:字符串、列表、元组
序列的特点:有序,可用下标索引来访问,支持切片操作。
一、字符串:
字符串的识别方式非常简单——有层名为【引号】的皮,只要是被【单/双/三引号】这层皮括起来的内容,
不论那个内容是中文、英文、数字甚至火星文。只要是被括起来的,就表示是字符串类型。
1、字符串拼接:
使用 + 将需要拼接的变量连在一起就行了。
例子:
1 a = 'hello'
2 b = 'world'
3 print(a + b)
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a2.py2 helloworld3
4 Process finished with exit code 0
2、查看数据类型的方法:
使用type()函数
1 print(type(123445))2 print(type(12345.0))3 print(type('12345'))4 print(type("12345"))
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a2.py2
3
4
5
6
7 Process finished with exit code 0
3、字符串中字母大小写转换
小写转成大写用upper()
大写转小写用lower()
两种用法是一样的
1 a = 'HELLO world'
2 #先输出变量a的值
3 print('原字符串a为:', a)4 #将变量a中全部字母转为大写
5 b =a.upper()6 print('全部转换为大写:', b)7 #将变量a中将全部字母转为小写
8 c =a.lower()9 print('全部转换为小写:', c)
运行结果
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a2.py2 原字符串a为: HELLO world3 全部转换为大写: HELLO WORLD4 全部转换为小写: hello world5
6 Process finished with exit code 0
4、字符串的格式化:
第一种方法:%操作符,早期版本使用
第二种方法:使用字符串对象的format()方法
第三种方法:使用f-strings,他的特点是进行字符串格式化时都是以 f 字母开头的。(推荐)
f-strings的运行速度很快。比%-string和str.format()这两种格式化方法都快得多
所以使用第三种f-strings
f-strings方法直接以f字母开头,后面紧跟你要输入的字符串内容编辑,无论是单引号,双引号,三个单引号括起来都可以,
然后把要输出的格式化变量内容对号入座即可
例一:
1 name = 'young'
2 age = '33'
3
4 string = f'我的名字是{name},我今年{age}岁'
5 print(string)
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a3.py2 我的名字是young,我今年33岁3
4 Process finished with exit code 0
例二:
在函数中使用
在f-string格式化中直接就像平时调用函数一样给函数传递参数,就能得到对应的结果,并把结果替换到先前指定的位置。
1 defadd(num1, num2):2 return num1 +num23
4
5 result = f'the result is {add(3, 7)}'
6 print(result)
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a5.py2 the result is 10
3
4 Process finished with exit code 0
例三:
多行格式化
如果我们想要输出的格式本身是以段落形式存在的,存在分行,那可以直接用三个单引号将整段内容括起来,随后输出的字符串格式也包含了段落分隔符。
1 name = 'young'
2 age = '33'
3 fruit = 'apple'
4
5 string = f'''我的名字是{name},6 我今年{age}岁,7 我喜欢吃{fruit}'''
8
9 print(string)
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a3.py2 我的名字是young,3 我今年33岁,4 我喜欢吃apple5
6 Process finished with exit code 0
如果想把结果输出为一行
只需输入反斜杠"\"跟在折行内容的末尾去做处理即可。
1 name = 'young'
2 age = '33'
3 fruit = 'apple'
4
5 string = f'''我的名字是{name},\6 我今年{age}岁,\7 我喜欢吃{fruit}'''
8
9 print(string)
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a5.py2 我的名字是young,我今年33岁,我喜欢吃apple3
4 Process finished with exit code 0
f -string提供了一种方法,可以在字符串文字中嵌入表达式,使用最小的语法。应该注意的是,f-string实际上是在运行时计算的表达式,而不是一个常量值。
在Python源代码中,f-string是一个文本字符串,前缀为f,其中包含括号内的表达式。表达式被替换为它们的值。
5、获取字符串的长度
例子:
1 #注意:空格也算一个长度
2 print(len('i love you 的意思是我爱你'))
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a5.py2 18
3
4 Process finished with exit code 0
6、通过索引切片获取字符串中的字符
第一个字符的索引位置是 0
1 fruit = 'apple'
2 print(fruit[0], fruit[3])
运行结果:
1 "D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a4.py2 a l3
4 Process finished with exit code 0
7、截取部分字符串
[ :] ([ :]表示步长)截取规律:冒号前代表的字符不变,冒号后的数:正数、负数都要往前(左)走一位。
[3 : ]冒号后什么也不输入表示截取到末尾
1 a = '123456,495'
2 print(a[1:3])3 print(a[:-1])
运行结果:
"D:\Program Files (x86)\python\python.exe" E:/python/python爬虫/从0开始、/a4.py23
123456,49Process finished with exit code 0