之前讲过python的数值类型
基本数据类型可以分为三类:数值类型、序列类型、散列类型
int、float、bool、str
1.1序列类型指的是一块可以存放多个值的连续内存空间,这些值按照一定的顺序排列,可以通过每个值所在的位置的编号(索引)访问它们
从左到右是从0开始
从右到左是从-1开始
例:访问CSDN博客论坛首元素和尾元素
str1 = "CSDN博客论坛" print(str1[0],"==",str1[-8]) print(str1[7],"==",str1[-1]) C == C 坛 == 坛1.2可变类型和不可变类型
主要的类型有:
不可变类型:数字、字符串、元组
可变类型:列表、字典、集合
s = "hello" print(s) s[0] = "a" print(s) #报错的原因是因为,"hello"字符串是不可变类型 Traceback (most recent call last): File "F:/Python36/PythonList/python44/day05/数据类型一.py", line 122, in1.3字符串s[0] = "a" TypeError: 'str' object does not support item assignment
用于存储和表示基本的文本信息,我们平时会用他来表示文本信息
例如:名字、地址、自我介绍
在单引号、双引号、三引号内包含的内容称之为字符串
三引号从功能上讲就是注释,作为文档字符串
1.3.1常用方法增删改查
1.3.1.1 find()方法的使用语法:
字符串.方法名()
范围性查找子串,返回索引值,没有就返回-1
语法:
str.find(str,beg=0,end=len(string))
str——》指定检索的字符串
beg——》开始索引,默认值是0
end——》结束索引,默认为字符串的长度
len——》字符串的长度
find()方法常用于检测字符串中是否包含子字符串str
s = "hello,world"
print(s.find("h")) # 返回的是索引值h的索引值是0
print(s.find("world")) #返回world首个字符的索引
print(s.find("worldd")) #返回-1
s = "python,helloworld,java,php"
print(s.find("h")) #返回首个h的索引值
find只能找一个子串的下标,默认是从下标0开始找到最后
s = "python,helloworld,java,php"
# print(s.find("h")) #返回首个h的索引值
print(s.find("h",8)) #找到第三个h
1.3.1.2 index()
检测字符串中是否包含字符串str,如果指定beg(开始)和end(结束)范围,则检测是否包含在指定的范围内,这个方法与find()方法一样,只不过如果str不在string中会报异常
s = "hello,world,python,php"
print(s.index("world"))
print(s.index("worldd"))
6
Traceback (most recent call last):
File "F:/Python36/PythonList/python44/day05/数据类型一.py", line 136, in
print(s.index("worldd"))
ValueError: substring not found
1.3.1.3 count()
返回找到的字符串个数
s = "hello,world,python,php"
print(s.count("h"))
3
1.3.1.4 strip()
去除字符串两边的空格、换行符、制表符,得到是一个新的字符串
code = input("请输入4位数的验证码:")
data = code.strip()
if data == "ABCD":
print("验证码正确")
else:
print("验证码错误")
请输入4位数的验证码: ABCD
验证码正确
1.3.1.5 startswith()、endswith()
判断字符串是否以xx开头,得到的是一个布尔值
zhuzhi = input("请输入住址:")
if zhuzhi.startswith("北京市"):
print("北京人口")
else:
print("非北京人口")
请输入住址:北京市朝阳区
北京人口
判断字符串是否以xx结尾,得到的是一个布尔值
zhuzhi = input("请输入地址:")
if zhuzhi.endswith("村"):
print("农业户口")
else:
print("非农业")
请输入地址:北京市朝阳区某某村
农业户口
1.3.1.6 isdigit
判断字符串是否是数字组成,返回结果为True或者False
v1 = input("请输入值1:")
v2 = input("请输入值2:")
if v1.isdigit() and v2.isdigit():
data = int(v1) + int(v2)
print(data)
else:
print("请输入正确的数字")
请输入值1:aa
请输入值2:aa
请输入正确的数字
1.3.1.7 lower()、upper()
字符串变大写/小写,得到的是一个新的字符串
lower()——》转小写
upper()——》转大写
s = "C" s1 = s.lower() print(s1) s2 = s1.upper() print(s2) c C1.3.1.8 split()
切分字符串,将字符串类型转列表,默认以空格切分,也可以指定字符切分
s = "my name is xiaochuan"
print(s.split())
s1 = "python,php,c,java"
print(s1.split(","))
['my', 'name', 'is', 'xiaochuan']
['python', 'php', 'c', 'java']
1.3.1.9 replace()
字符串内容的替换,得到一个新的字符串
content = input("请输入评论信息:")
content1 = content.replace("草","*")
print(content1)
请输入评论信息:草aaa
*aaa
1.3.1.10 join()
用于将序列中的元素以指定字符串连接生成一个新的字符串
常用与将列表类型转换为字符串
1.3.2字符串的公共功能就是说字符串可以用,其他的类型也能用
字符串是一个有序的字符集合,按照顺序排列数据类型 python都为其赋予了索引的概念
索引是从零开始的
1.3.2.1正向取(从左到右)、反向取(从右到左,负号)str1 = "hello python" # 正向取 print(str1[0]) print(str1[6]) # 反向取 print(str1[-4])
正着从0算,倒着从-1算,如果超出了 报错 下标越界
str1 = "hello python" print(str1[len(str1)-1]) # 最后一个 print(str1[-len(str1)]) #第一个1.3.2.2切片(顾头不顾尾,步长)
顾头不顾尾:取出索引0到8的所有字符
str1 = "hello python" #注意空格也算 print(str1[0:9]) hello pyt
步长:0:12:2,第三个参数2代表步长,会从0开始,每次累加一个2即可
str1 = "hello python" print(str1[0:12:2]) hlopto
反向切片
str1 = "hello python" print(str1[::-1]) nohtyp olleh本期就讲解这么多啦,下期见!
(我的联系方式:1056546915(QQ))


![[4]在python中代表啥_python中{:>4}? [4]在python中代表啥_python中{:>4}?](http://www.mshxw.com/aiimages/31/786662.png)
