6.1_5 Python3.x入门 P6 【基础】不可变序列(元组tuple、字符串str)

相关链接



一、元组tuple -> (v1,v2,…)

方法总结
序号分类关键字/函数/方法说明
1查找元组.index(数据)值->索引,返回数据出现位置的索引
元组[索引]索引->值,返回指定索引位置的数据
遍历for i in 元组
索引->值,依次获取每个索引对应的值
2增加元组= 元组1 + 元组2将元组2的数据追加到元组1,并返回一个新的元组
3修改-可以通过函数 list(元组) 转为list后操作
4删除del 元组删除元组,将元组从内存中清除
5统计len(元组)元组长度
元组.count(数据)数据在元组中出现的次数
6排序sorted(元组)升序排序(返回一个列表,Python内置函数)
sorted(元组,reverse=True)降序排序(返回一个列表,Python内置函数)
7判断数据 in 元组判断元组中是否有指定数据,返回布尔类型值
数据 not in 元组判断元组中是否无指定数据,返回布尔类型值
8切片元组[start:end:step]对元组切片,返回一个新的元组,语法与list切片相同,
参考 P4 【基础】可变序列(列表list、字典dict、集合set)
->【2.8 切片】
类型
t = ('python', 'hello', 90)
print(type(t))  # <class 'tuple'>
print(t)  # ('python', 'hello', 90)
创建语法
"""
 @author GroupiesM
 @date 2022/4/28 17:55
 @introduction

元组创建方式:
    方式一:使用小括号(),元素之间英文逗号分隔;只有一个元素时也要加逗号,否则数据类型为基本数据类型
        tup = ('python',)
        tup = (1,3,5)
    方式二:内置函数 tuple() 类型转换
        tuple(('hello','php'))                   # list -> tuple
        tuple({'name': '张三', 'age': 100})       # 字典 -> tuple
        tuple(['hello','php'])                   # 元组 -> tuple
        tuple({'python', 'hello', 90})           # 集合 -> tuple
        tuple('hello')                           # 字符串->tuple
"""
'''方式一'''
tup = ('hello', 'python')
print(tup)  # ('hello', 'python')

'''方式二'''
'''1.list->tuple'''
tup1 = tuple(('hello', 'php'))  # list -> tuple
print(1, tup1)  # ('hello', 'php')

'''2.字典->tuple时,自动取字典的key'''
tup2 = tuple({'name': '张三', 'age': 100})  # 字典 -> tuple
print(2, tup2)  # ('name', 'age')

'''3.元组->tuple'''
tup3 = tuple(['hello', 'php'])  # 元组 -> tuple
print(3, tup3)  # ('hello', 'php')

'''4.集合->tuple'''
tup4 = tuple({'python', 'hello', 90})  # 集合 -> tuple
print(4, tup4)  # ('python', 'hello', 90)

'''5.字符串->tuple'''
tup5 = tuple('hello')  # 字符串->tuple
print(5, tup5)  # ('h', 'e', 'l', 'l', 'o')
注意
#只有一个元素时,括号需要添加逗号,否则元组定义的小括号会被解释器自动忽略
tup = (5)
print(type(tup))  # <class 'int'>

tup = (5,)
print(type(tup))  # <class 'tuple'>

# 空元组
tup = ()
print(type(tup))  # <class 'tuple'>
元组特点

1. 元组的数据有序
2. 元组的数据可以重复
3. 元组的数据可以任意类型混合存储
4. 元组的数据不可以修改


1.1 查找

索引:索引从0开始计数,表示在列表中的位置编号索引也叫下标
  正向索引:从0到n-1 list[0] -> list[n-1],如下图n=4,list[0]->list[3]
  逆向索引:从-n到-1 list[-n] -> list[-1] ,如下图n=4,list[-4]->list[-1]
   在这里插入图片描述
注意:从列表中取值时,如果超出索引范围,程序会报错,抛出IndexError

a. index() - 值 -> 索引

"""
index():
    如果列表中存在n个相同元素,只返回相同元素中第一个元素索引
    如果查询的元素在列表中不存在,则会抛出ValueError
"""

tup = ('hello', 'python', 'python', 50, True)
'''索引位置
          0        1           2      3    4
        ['hello', 'python', 'python', 50, True]
'''

num: int = tup.index('python')
print(num) # 1

num: int = tup[2:4].index('python')
print(num) # 0 先切片,对切片后的列表再查找

num: int = tup.index(50)
print(num) # 3

num: int = tup.index('50')
print(num) # ValueError: '50' is not in list

b. 元组[索引] - 索引 -> 值

tmp = ('hello', 'python', 'python', 50, True)

print(tmp[0])  # hello
print(tmp[1])  # python

c. 遍历

  元组中的数据类型一般不同,所以在实际开发中,一般不会对元组进行遍历操作。

t = ('Python', 'hello', 90)
for i in t:
    print(i, end="\t")  # Python	hello	90

print()
for i in range(0, len(t)):
    print(t[i], end="\t")  # Python	hello	90

1.2 增加

a. 元组 = 元组1 + 元组2

两个tuple可以相加 (+),但是不可以做减法(-)。

tup1 = (1, 2, 3, 4, 5, 6)
tup2 = (5, 6, 7, 8)
tup = tup1 + tup2 + tup2
print(tup)  # (1, 2, 3, 4, 5, 6, 5, 6, 7, 8, 5, 6, 7, 8)

# tuple类型、list类型 不能混合相加
lst2 = [5, 6, 7, 8]
tup = tup1 + lst2 # TypeError: can only concatenate tuple (not "list") to tuple
print(tup)

二、字符串str -> ‘’

1. 字符串在python中用于表示文本,数据类型为<class ‘str’>
2. 在Python中可以使用一对双引号" 或者一对单引号'定义一个字符串
3. 当字符串内容包含"或者'时,可以用以下方式处理
  1) 使用\"或者\'做字符串的转义(实际开发中一般不这么做)

  2) 如果字符串内部需要使用",可以使用'定义字符串
  3) 如果字符串内部需要使用',可以使用"定义字符串

方法总结
序号分类关键字/函数/方法说明
1查找字符串.index(子字符串)查找子串substr第一次出现的位置,
如果查找的子串不存在时,则抛出ValueError
字符串.rindex(子字符串)查找子串substr最后一次出现的位置,
如果查找的子串不存在时,则抛出ValueError
字符串.find(子字符串)查找子串substr第一次出现的位置,
如果查找的子串不存在时,则返回-1
字符串.rfind(子字符串)查找子串substr最后一次出现的位置,
x如果查找的子串不存在时,则返回-1
字符串[索引]索引->值,返回指定索引位置的字符
遍历for i in 字符串
索引->值,依次获取每个索引对应的字符
2增加字符串 = 字符串1 + 字符串2将字符串2的数据追加到字符串1,并返回一个新的字符串
字符传 = 字符串1 * 数量n将字符串1重复n次追加到自己,并返回一个新的字符串
3修改-可以通过函数 list(元组) 转为list后操作
4删除del 字符串删除字符串,将字符串从内存中清除
5统计len(字符串)字符串长度
字符串.count(子字符串)子串在字符串中出现的次数
6排序sorted(字符串)升序排序(返回一个列表,Python内置函数)
sorted(元组,reverse=True)降序排序(返回一个列表,Python内置函数)
7判断子字符串 in 字符串判断字符串中是否有指定子串,返回布尔类型值
子字符串 not in 字符串判断字符串中是否无指定子串,返回布尔类型值
8切片字符串[start:end:step]对元组切片,返回一个新的元组,语法与list切片相同,
参考 P4 【基础】可变序列(列表list、字典dict、集合set)
->【2.8 切片】

2.1 简介

"""
 @author GroupiesM
 @date 2022/5/9 14:03
 @introduction <class 'str'>

 列表:list = ['hello', 'world', 100]
 字典:dict = {'name': '张三', 'age': 100}
 元组:tuple = ('python','hello',90)
 集合: set  = {'python', 'hello', 90}
 字符串: str = 'python'
"""
'''
字符串定义:有三种方式,单引号、双引号、三引号
'''
str1 = 'python'
str2 = "python"
str3 = ''''python'''
print(type(str1))  # <class 'str'>
print(str1)  # ('python', 'hello', 90)

2.2 驻留机制

"""
 @author GroupiesM
 @date 2022/5/9 14:14
 @introduction
一、驻留机制
    1)python中字符串是基本类型,是不可变字符序列
    2)什么是字符串驻留机制 【相同字符串的内存地址也相等】
        仅保存一份相同且不可变字符串的方法,不同的值被存放在字符串的驻留池中,
        Python的驻留机制对相同的字符串只保留一份拷贝,后续创建相同字符串时,
        不会开辟新空间,而是把该字符串的地址赋值给新建的变量

二、注意事项
    1、字符串的驻留条件
        仅包含字母、数字、下划线的字符串,python会启用驻留机制,
        因为Python解释器仅对看起来像python标识符的字符串使用intern()方法,而python标识符正是由字母、数字和下划线组成。
    2、字符串的连接
        字符串数据类型,是不可变类型,我们都知道,如果我们有了一个字符串,想要修改它,是不可以的,则必须新建一个对象,
        因此不推荐使用“+”连接字符串,而推荐大家采用join()方法
    3、整数驻留
        python会针对整数范围是[-5, 256]的整数启用驻留机制(python认为这些数字时常用数字,放在缓存中节省开销)
	4、在pycharm和黑窗口(iterm)中测试结果不同,具体原因不详
		1)teminal:不触发字符串驻留机制
    	2)pycharm:触发字符串驻留机制
    	
P.S 比较运算符:
    == ->     value的比较
    is,is not 内存地址比较
"""
str1 = "abc"
str2 = "abc"	# 相同字符串的内存地址也相等 -> str1与str2内存地址相等
str3 = "".join(['ab', 'c'])
print(str1, type(str1), id(str1))  # abc <class 'str'> 4378617712
print(str2, type(str2), id(str2))  # abc <class 'str'> 4378617712
                                        # 结论: str1、str2的地址相等
print(str3, type(str3), id(str3))  # abc <class 'str'> 4381017264

"""
在不同场景测试包含[\w\d_]以外的字符时,python字符串驻留机制(python3.8.9)
    1)terminal:不触发字符串驻留机制
    2)pycharm:触发字符串驻留机制
"""

"""1) terminal:
groupiesm@bogon ~ % python3
Python 3.8.9 (default, Feb 18 2022, 07:45:33)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> str4='abc%'
>>> str5='abc%'
>>> str4==str5
True
>>> str4 is str5
False
>>>
"""

"""2) pycharm:"""
str4 = 'abc%'
str5 = 'abc%'
print(str4 == str5)  # True 比较value
print(id(str4)) # 4300391152
print(id(str5)) # 4300391152
print(str4 is str5)  # True 比较内存地址

2.3 查询

"""
 @author GroupiesM
 @date 2022/5/23 17:01
 @introduction
 查询方法:
    index(): 查找子串substr第一次出现的位置,如果查找的子串不存在时,则抛出ValueError
    rindex():查找子串substr最后一次出现的位置,如果查找的子串不存在时,则抛出ValueError
    find():查找子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1
    rfind():查找子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1
"""
s = 'hello,hello'
print(s.index('lo'))  # 3
print(s.rindex('lo'))  # 9
print(s.find('lo'))  # 3
print(s.rfind('lo'))  # 9

# print(s.index('k'))  #报错 如果查找的子串不存在时,则抛出ValueError
# print(s.rindex('k'))  #报错 如果查找的子串不存在时,则抛出ValueError
print(s.find('k'))  # -1
print(s.rfind('k'))  # -1

2.4 大小写转换

"""
 @author GroupiesM
 @date 2022/6/20 09:34
 @introduction
 大小写转换:
    upper(): 把字符串中所有的字符都转成大写字母
    lower(): 把字符串中所有的字符都转成小写字母
    swapcase(): 把字符串中所有大写字母转成小写字母,把所有小写字母转成大写字母
    capitalsize(): 整个字符串首字母大写,其余字母小写
    title(): 每个单词首字母大写,其余字母小写
"""
s = 'hEllo WoRld'
print(s, id(s))  # hEllo WoRld 4333433968
print(s.upper(), id(s.upper()))  # HELLO WORLD 4333137520
print(s.lower(), id(s.lower()))  # hello world 4333137520
print(s.swapcase(), id(s.swapcase()))  # HeLLO wOrLD 4333137520
print(s.capitalize(), id(s.capitalize()))  # Hello world 4333137520
print(s.title(), id(s.title()))  # Hello World 4333137520

2.5 内容对齐

"""
 @author GroupiesM
 @date 2022/6/20 10:33
 @introduction
 内容对齐:
    center(): 居中对齐,第1个参数指定宽度,第2个参数指定填充符,第2个参数是可选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串
    ljust(): 左对齐,第1个参数指定宽度,第2个参数指定填充符,第2个参数是可选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串
    rjust(): 右对齐,第1个参数指定宽度,第2个参数指定填充符,第2个参数是可选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串
    zfill(): 右对齐,左边用0填充,第1个参数指定宽度,如果设置宽度小于实际宽度则返回原字符串
"""
s = 'hello world'
'''指定填充'''
print(s.center(20, '*'))  # ****hello world*****
print(s.ljust(20, '&'))  # hello world&&&&&&&&&
print(s.rjust(20, '^'))  # ^^^^^^^^^hello world
print(s.zfill(20))  # 000000000hello world

'''不指定填充'''
print(s.center(20))  #    hello world
print(s.ljust(20))  #hello world
print(s.rjust(20))  #         hello world
print(s.zfill(20))  # 000000000hello world

'''指定宽度 < 字符串长度'''
print(s.center(5))  # hello world
print(s.ljust(4))  # hello world
print(s.rjust(3))  # hello world
print(s.zfill(2))  # hello world

2.6 split()-分割

"""
 @author GroupiesM
 @date 2022/6/20 10:44
 @introduction
 split()-分割:
    split(self,sep,maxsplit):从字符串左边开始分割,返回一个列表
        self:字符串本身
        sep:指定分隔符,默认为空格
        maxsplit:指定最大分割次数,分割指定次数后,剩余字符串作为一个整体不再分割
    rsplit():从字符串右边开始分割,返回一个列表(参数相同)
"""

s = 'a1b1c d1e1f g h'
print(s.split())  # ['a1b1c', 'd1e1f', 'g', 'h']
print(s.split(sep='1'))  # ['a', 'b', 'c d', 'e1f']
print(s.split(maxsplit=3))  # ['a', 'b', 'c d', 'e', 'f g h']
print(s.split(sep='1', maxsplit=3))  # ['a', 'b', 'c d', 'e1f g h']

print('----')
print(s.rsplit())  # ['a1b1c', 'd1e1f', 'g', 'h']
print(s.rsplit(sep='1'))  # ['a', 'b', 'c d', 'e', 'f g h']
print(s.rsplit(maxsplit=3))  # ['a1b1c', 'd1e1f', 'g', 'h']
print(s.rsplit(sep='1', maxsplit=3))  # ['a1b', 'c d', 'e', 'f g h']

2.7 判断

"""
 @author GroupiesM
 @date 2022/6/20 11:21
 @introduction
 判断:
    isidentifier(): 合法的变量名(可用作接收变量的变量名)
    isspace(): 全部由空字符串组成(回车、换行、水平制表符)
    isalpha(): 全部由字母组成
    isdecimal(): 全部由十进制的数字组成
    isnumeric(): 全部由数字组成(中文简体、繁体,符号,罗马数字)
    isalnum(): 全部由字母和数字组成
"""
print('---isidentifier() 合法的变量名(可用作接收变量的变量名)---')
print('if'.isidentifier())  # True
print('_b'.isidentifier())  # True
print('张三'.isidentifier())  # True
print('8a'.isidentifier())  # False
print(''.isidentifier())  # False

print('---isspace() 全部由空字符串组成(回车、换行、水平制表符)---')
print(' \r\t\n'.isspace())  # True
print(' \r\t\n0'.isspace())  # False

print('---isalpha() 全部由字母组成---')
print('abc'.isalpha())  # True
print('张三'.isalpha())  # True
print('张三1'.isalpha())  # False

print('---isdecimal(): 全部由十进制的数字组成---')
print('123'.isdecimal())  # True
print('12三'.isdecimal())  # True
print('123.4'.isdecimal())  # False
print('123 '.isdecimal())  # False

print('---isnumeric(): 全部由数字组成(中文简体、繁体,符号,罗马数字)---')
print('12'.isnumeric())  # True
print('12②贰Ⅱ'.isnumeric())  # True
print('1two'.isnumeric())  # False

print('---isalnum(): 全部由字母和数字组成---')
print('张三8'.isalnum())  # True
print('张三8 '.isalnum())  # False

2.8 替换

"""
 @author GroupiesM
 @date 2022/6/20 12:04
 @introduction
 替换:
    replace(String old,String new):
    replace(String old,String new,Integer count):
        self: 默认字符串本身
        1.old: 替换前的字符串
        2.new: 替换后的字符串
        3.count: 最大替换次数,(可省略参数)
"""
s = '12 12 12'
print(s.replace(' ', '\r\n', 1))
'''
12
12 12
'''
print('---')
print(s.replace(' ', '\r\n'))
'''
12
12
12
'''

2.9 字符串合并

"""
 @author GroupiesM
 @date 2022/6/20 12:12
 @introduction
 合并:
    join(): 将列表或元组中的字符串合并成一个字符串,列表或元组的元素必须都是字符串
"""
lst =['1 ','2 ','345']
print(''.join(lst)) # 1 2 345

lst =['1 ','2 ',345]
print(''.join(lst)) # TypeError: sequence item 2: expected str instance, int found

2.10 比较

"""
 @author GroupiesM
 @date 2022/6/21 16:57
 @introduction
 比较:
    运算符:>,>=,<,<=,==,!=
        比较规则:首先比较两个字符串中的第一个字符,如果相等则继续比较下一个字符,
        依次比较下去,直到两个字符串中的字符不相等时,其比较结果就是两个字符串的
        比较结果,两个字符串中所有的后续字符将不再被比较
    比较原理:
        两个字符进行比较,比较的是其 ASCII码值
        通过内置函数ord(字符),可以获取字符对应的 ASCII码值(数字)
        通过内置函数chr(数字),可以获取数字对应的 字符
"""
'''>'''
print("apple" > "app")  # True
print('a' > '张')  # False
print(ord('a'))  # 97
print(ord('张'))  # 24352
print(chr(97))  # a

'''
==与is的区别
    ==比较的是value
    is比较的是id是否相等(内存地址)
'''
a = 'ab'
b = "".join(['a', 'b'])

'''a与b的 值、内存地址'''
print(a, id(a)) # ab 4375883376
print(b, id(b)) # ab 4377252400

print(a == b)  # True :a与b值相等
print(a is b)  # False :a与b内存地址不相等

2.11 切片

"""
 @author GroupiesM
 @date 2022/6/22 11:28
 @introduction
 切片:
    1.字符串是不可变类型,不具备增、删、改等操作
    2.字符串是不可变类型,切片操作将产生新的对象

 假设有字符串 str = 'hello,python',已知str长度为11
    0   1   2   3  4  5  6  7  8  9  10  11  [正向索引]
    h   e   l   l  o  ,  p  y  t  h  o   n
   -11 -10 -9  -8 -7 -6 -5 -4 -3 -2 -1       [反向索引] 了解即可
 切片语法1:newStr = str[起始(x):结束(y):步长(s)]
    其中x,y为任意整数,表示索引位置
    其中s为任意整数,表示步长(每次取字符间隔几个位置)
    1. 起始(x)可以省略不写,不写则取默认值 0;例如 str[:5] <=> str[0:5]
    2. 结束(y)可以省略不写,不写则取默认值 字符串结尾位置; 例如str[6:] <=> str[6:11]
    3. 步长(s)可以省略不写,不写则取默认值 1; 例如str[:5:] <=> str[:5:1]
    4. 若 起始(x)>结束(y),生成新的字符串为空字符串 ''
    5. 若 0 < 起始(x) < 结束(y),则按照 正向索引 进行切片操作
    6. 若 起始(x) < 结束(y) < 0,则按照 反向索引 进行切片操作。基本没有使用场景,了解即可
    7. 若 s<0,则字符串反向切片
"""
str = 'hello,python'
s1 = str[:5]  # str[:5] <=> str[0:5] 两种写法等价
print('s1', s1)  # hello

s2 = str[6:]  # str[6:] <=> str[6:11] 两种写法等价
print('s2', s2)  # python

s3 = str[:]  # str[:] <=> str[0:11] 两种写法等价
print('s3', s3)  # hello,python

s4 = str[0:5:]  # str[0:5:] <=> str[0:5:1] 两种写法等价
print('s4', s4)  # hello

s5 = str[-5:-1]
print('s5', s5)  # s5 ytho

s6 = str[0:5:1]
print('s6', s6)  # hlo

s7 = str[-15:-1]  # s<0,反向切片
print('s7', s7)  # nohtyp,olleh

2.12 字符串编解码

"""
 @author GroupiesM
 @date 2022/6/27 16:48
 @introduction
 字符串编解码:
    目的:用于网络中的数据传输(byte字节传输)

    encode(String encoding):编码,将字符串转换为二进制数据(bytes)
    decode(String encoding):解码,将bytes类型的数据转换为字符串类型

    P.S
        1.编码方式:常见编码方式有 GBK、UTF-8
        2.乱码最常见的原因就是编解码不一致
"""
s = '将bytes类型的数据转换为字符串类型'

# 编码 - b'\xbd\xabbytes\xc0\xe0\xd0\xcd\xb5\xc4\xca\xfd\xbe\xdd\xd7\xaa\xbb\xbb\xce\xaa\xd7\xd6\xb7\xfb\xb4\xae\xc0\xe0\xd0\xcd'
g_encode = s.encode('GBK')
g_encode = s.encode(encoding='GBK')
print(g_encode)

# 解码 - 将bytes类型的数据转换为字符串类型
g_decode = g_encode.decode('GBK')
g_decode = g_encode.decode(encoding='GBK')
print(g_decode)

22/06/28

M

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值