一、字符串
字符串是 Python 中最常用的数据类型,用引号('或")来包裹着的。
1. 创建
str1 = "hello world"
str2 = 'hello python'
2. 字符串的切片
语法:字符序列[开始位置 : 结束位置 : 步长]
注意:选区范围左闭右开;如果步长省略不写则默认为1;如果省略开始位置则从0开始;如果不写结束位置则默认结束到尾部;如果步长为负数,表示从后往前;如果选取方向不一致,则选不出来;
str1 = "hello world"
print(str1[1:3]) #el
print(str1[0:4:2]) #hl
print(str1[-1:-4:-1]) #dlr
3. 查找
a. find()
作用:检测某个字串是否包含在这个字符串中,如果在就返回子串开始的位置下标,否则返回-1
语法:字符序列.find('子字符', 开始位置 , 结束位置)
str1 = "hello world"
print(str1.find("e")) # 1
print(str1.find("m")) # -1
b. index()
作用:检测某个字串是否包含在这个字符串中,如果在就返回子串开始的位置下标,但是如果查找失败则报错
语法:字符序列.index(子串,开始查找位置,结束查找位置)
str1 = "hello world"
print(str1.index("e")) # 1
print(str1.index("m")) # 报错
c. count()
作用:查找字串的个数,返回字串的个数
语法:字符序列.count(字串, 开始查找位置, 结束查找位置)
str1 = "hello world"
print(str1.count("l")) # 3
print(str1.count("m")) # 0
d. rfind()
作用:从右边开始查找,检测某个字串是否包含在这个字符串中,如果在就返回子串开始的位置下标
语法:字符序列.rfind(字串, 开始查找位置, 结束查找位置)
str1 = "hello world"
print(str1.rfind("l")) # 9
print(str1.rfind("m")) # -1
e. rindex()
作用:从又边开始,检测某个字串是否包含在这个字符串中,如果在就返回子串开始的位置下标,但是如果查找失败则报错
语法:字符序列.rindex(字串, 开始查找位置, 结束查找位置)
str1 = "hello world"
print(str1.rindex("l")) # 9
print(str1.rindex("m")) # 报错
4. 修改
a. replace( )
作用:字符串替换,有返回值
语法:字符序列.replace(旧字串,新字串,替换次数)
str1 = "hello world"
str2 = str1.replace("o","tt") # helltt wttrld
str3 = str1.replace("o","rr",1) # hellrr world
b. split( )
作用:字符串的分割,返回列表(分割符丢失)
语法:字符序列.split(分割符,分割的次数)
str1 = "hello world"
str2 = str1.split("o") # ['hell', ' w', 'rld']
str3 = str1.split("o",1) # ['hell', ' world']
c. join( )
作用:字符串的拼接,合并列表里面的字符串数据为一个大字符串
语法:拼接符.join(多字符串组成的序列)
str1_list = ["d","t","e"]
str2 = "".join(str1_list) # dte
str3 = "P".join(str1_list) # dPtPe
d. 大小写转换
1). capilalize( ) 作用: 字符首字母大写
2). title( ) 作用: 字符串中每个单词首字母大写
3).upper( ) 作用: 转化为大写
4).lower( ) 作用: 转化为小写
str1 = "hello world"
str2 = "Hello World"
print(str1.capitalize()) # Hello world
print(str1.title()) # Hello World
print(str1.upper()) # HELLO WORLD
print(str2.lower()) # hello world
e. 删除空白字符串
1). strip( ) 作用:除去空白的字符串
2). lstrip( ) 作用:除去左边空白的字符串
3). rstrip( ) 作用:出去右边空白的字符串
str1 = " hello world "
print(str1.strip()) # hello world
print(str1.lstrip()) # hello world
print(str1.rstrip()) # hello world
f. 字符串对齐
1). ljust( ) 作用:左对齐 语法:字符串.ljust(设置长度,填充字符)
2). rjust( ) 作用:右对齐
3). center( ) 作用:居中对齐
str1 = "hello world"
print(str1.ljust(16,"r")) # hello worldrrrrr
print(str1.rjust(16,"r")) # rrrrrhello world
print(str1.center(16,"r")) # rrhello worldrrr
5. 判断
a. 判断开头结尾
1)startswith( )
作用:判断字符串是否以某个字符串开头,返回值是布尔类型
语法:字符串序列.startswith(字串,开始位置下标,结束位置下标)
2)endswith( )
作用:判断字符串是否以某个字符串结尾,返回值是布尔类型
语法:字符串序列.endswith(字串,开始位置下标,结束位置下标)
str1 = "hello world"
print(str1.startswith("h")) # True
print(str1.endswith("h")) # False
b. 判断
1). isalpha( )
作用:判断是否全为字母
2). isdigit( )
作用:判断是否全为数字
3). isalnum()
作用:判断是否是数字字母组成
4). isspasce()
作用:判断是否为空白
str1 = "helloworld2019"
print(str1.isalpha()) # False
print(str1.isdigit()) # False
print(str1.isalnum()) # True
print(str1.isspace()) # False
二、 列表
一次性存储多个数据,可以有增删查改
1. 创建
list1 = [] # []
list2 = [1,3,4] # [1,3,4]
list3 = list() # []
2. 查找
a. 下标索引
name_list = ["tom", "lily"]
print(name_list[0]) # tom
b. 函数
1) index( )
作用:返回指定的数据所在的位置的下标,假如不在列表则报错
语法:列表序列.index( 数据, 开始位置下标,结束位置的下标)
name_list = ["tom", "lily","yanggou"]
print(name_list.index("lily")) # 1
2) count( )
作用:返回指定数据在列表中出现的次数
语法:列表序列.count(数据)
list1 = [1,2,3,1,2,1]
print(list1.count(1)) # 3
3) len( )
list1 = [1,2,3,1,2,1]
print(len(list1)) # 6
3. 增加
1) append( )
作用:列表结尾追加数据(追加数据的时候,如果数据是序列,追加整个序列到尾部)
语法:列表序列.append(数据)
list1 = [1,2,3]
list1.append("abc")
print(list1) # [1, 2, 3, 'abc']
2) extend( )
作用:列表结尾追加数据(追加数据的时候,如果数据是序列,会将序列内容拆开逐一追加)
语法:列表序列.extend(数据)
list1 = [1,2,3]
list1.extend("abc")
print(list1) # [1, 2, 3, 'a', 'b', 'c']
3) insert( )
作用:在列表里指定位置添加数据
语法:列表序列.insert(下标,数据)
list1 = [1,2,3]
list1.insert(1,"abc")
print(list1) # [1, 'abc', 2, 3]
4. 删除
1) del
语法:del 目标 或者 del(目标)
list1 = [1,2,3]
del(list1[1])
print(list1) #[1, 3]
del list2
print(list2)
2) pop( )
作用:删除指定下标的数据,如果不指定则删除最后一个数据
语法:列表序列.pop(下标)
list1 = [1,2,3]
t = list1.pop(2) # 3
print(list1) # [1, 2]
3) remove( )
语法:列表序列.remove(数据)
list1 = [1,2,3]
list1.remove(2)
print(list1) # [1, 3]
4)clear( )
语法:列表序列.clear()
list1 = [1,2,3]
list1.clear()
print(list1) # []
5. 修改
1) 下标修改
语法:列表序列[索引值] = 数据
list1 = [1,2,3]
list1[1] = 100
print(list1) #[1, 100, 3]
2) reverse()
作用;将序列逆序排列
语法:列表序列.reverse()
list1 = [4,2,6,5]
list1.reverse()
print(list1) # [5, 6, 2, 4]
3) sort()
作用:排序,升序,降序。
语法:列表序列.sort(,key=None, reverse=Fales)。reverse=Fales默认为升序,=True为降序
list1 = [4,2,6,5]
list1.sort()
print(list1) # [2, 4, 5, 6]
三、元组
查找
1)按下标查找
tuple1 = (4,2,6,5)
tuple1[2]
print(tuple1) # (4, 2, 6, 5)
2)index( )
tuple1 = (4,2,6,5)
print(tuple1.index(2)) # 1
3)count( )
tuple1 = (4,2,6,5,2)
print(tuple1.count(2)) # 2
4)len( )
tuple1 = (4,2,6,5,2)
print(len(tuple1)) # 5
四、字典
1. 创建
dict1 = {}
dict2 = {"name":"moko", "age":18}
dict3 = dict()
2. 增加
语法:字典序列[ key ] = 值
注意:如果key存在则修改这个key对应的值;如果不存在则新增数据
dict1 = {"name":"moko","age":18}
dict1["id"] = 1234
print(dict1) # {"name":"moko","age":18,"id":1234}
3. 删除
a. del
key存在则删除,不存在则报错
dict1 = {"name":"moko","age":18}
del dict1["name"]
print(dict1) #{'age': 18}
b. clear()
清空字典
dict1 = {"name":"moko","age":18}
dict1.clear()
print(dict1) # {}
4. 修改
语法:字典序列[ key ] = 值
dict1 = {"name":"moko","age":18}
dict1["name"] = "mmk"
print(dict1) # {'name': 'mmk', 'age': 18}
5. 查找
a. key值查找
dict1 = {"name":"moko","age":18}
print(dict1["name"]) # moko
b. get( )
dict1 = {"name":"moko","age":18}
print(dict1.get("name")) # moko
c. keys( )
dict1 = {"name":"moko","age":18}
for i in dict1.keys(): # name age
print(i,end=" ")
d. values( )
dict1 = {"name":"moko","age":18}
for i in dict1.values(): # moko 18
print(i,end=" ")
e. items( )
dict1 = {"name":"moko","age":18}
for i,j in dict1.items(): # ('name', 'moko') ('age', 18)
print((i,j),end=" ")
五、集合
1. 创建集合
set1 = set()
set2 = {1,2,3}
2. 增加
a. add()
set1 = {1,2,3}
set1.add(7)
print(set1) # {1, 2, 3, 7}
b. update()
追加的数据是序列
set1 = {1,2,3}
set1.update([7,8,9])
print(set1) # {1, 2, 3, 7, 8, 9}
3. 删除
a. remove()
set1 = {1,2,3}
set1.remove(2)
print(set1) # {1, 3}
set2 = {1,2,3}
set2.remove(8)
print(set2) # 报错
b. discard()
set1 = {1,2,3}
set1.discard(2)
print(set1) # {1, 3}
set2 = {1,2,3}
set2.discard(8)
print(set2) # {1,2,3}
d.pop
随机删除集合中的某个数据,并返回这个数据
set1 = {1,2,3}
set1.pop()
print(set1) # {2, 3}
4. 查找
in:判断数据在集合序列
not in:判断数据不在集合序列
s1 = {10, 20, 30, 40, 50}
print(10 in s1)
print(10 not in s1)
1 2 3 4