- 一旦赋值,内容不可改变
- 常用来保存不可变的数据
- CPU和内存的使用更少
fruits =("apple", "banana", "cherry")
print(type(fruits)) # <class 'tuple'>
print("apple" in fruits) # 成员运算
fruits2 = ("lemon",)
fruits3 = fruits + fruits2 # 合并运算
print(fruits3)
print(fruits[2]) # 读取
print(fruits[1:3]) # 切片,得到的还是元组
例子1
该例子可以很容易的用于去掉一个最高分,去掉一个最低分
a, *b, c = 1, 2, 3, 4, 5
print(b)
字符串
定义:由零个或多个字符组成的有限序列
字符串的创建
a = "hello world" # 这种方式不能换行
c = '''
hello,
goodbye
'''
# 这样输入和输出格式是一致的
如果字符串中带有单引号or双引号,需要加入转义字符\
b = "\"\"hello\"\""
print(b) # ""hello""
注意,如果需要输出正确路径,要注意反斜杠的应用
d = "C:\\Users\\Administrator\\Documents"
print(d)
# C:\Users\Administrator\Documents
也可以加上r,表示字符串中没有转义
e = r"C:\Users\Administrator\Documents"
print(e)
带格式化的字符串
f = f"文件路径:{d}"
print(f)
UTF-8
字符编码的发展:ASCII—>GB2312—>GBK—>Unicode
UTF-8支持大多数国家的文字,Python中的字符串也可以用\u来创建
str1 = "\u9a55\u9a99"
print(str1)
字符串运算
a = "hello world"
b = "bye world"
print("lo" in a) # True
print(a == b) # False
print(len(a))
字符串遍历每个字符
for i in range(len(a)):
print(a[i])
for i in a:
print(i)
字符串的索引和切片
print(a[0], a[-len(a)]) # h h
print(a[2:5]) # llo
例子2:滚动文字效果
需要在Terminal执行
import time
import os
content = "abcdefghijklmnopqrstuvwxyz "
while True:
os.system("cls")
print(content)
time.sleep(0.3)
content = content[1:] + content[0]
字符串的常用方法
大小写
print(a.upper()) # 全大写
print(a.lower()) # 全小写
print(a.capitalize()) # 首字母大写
print(a.title()) # 所有单词首字母大写
性质判断
print(a.isdigit()) # 是否数字构成的
print(a.isalpha()) # 是否字母
print(a.isalnum()) # 是否数字和字母
print(a.isascii()) # 是否ascii,需要python3.7
print(a.startswith("h")) # 是否以h开头
print(a.endswith("d")) # 是否以d结尾
字符查找
index如果没找到,程序会报错
print(a.index("love")) # 从左往右找
print(a.index("love", 10)) # 从左往右找,指定从10开始找
print(a.rindex("love")) # 从右往左找
find如果没找到,会返回-1
print(a.find("love")) # 从左往右找
print(a.find("love", 10)) # 从左往右找,指定从10开始找
print(a.rfind("hi")) # 从右往左找
格式输出
a = "hello, World."
print(a.center(80, "~")) # 居中
print(a.rjust(80, "-")) # 左对齐
print(a.ljust(80, "=")) # 右对齐
b = "zzz"
print(b.zfill(6)) # 以0填充
c = 123456
print(f"{c:.1f}") # 保留一位小数
print(f"{c:.2%}") # 百分比显示
print(f"{c:0>10d}") # 左边补0,补满10位
print(f"{c:.2e}") # 科学计数法 1.23e+05
字符串修剪
email = " abc@test.com "
print(email.strip()) # 修剪左右两侧的空格
print(email.lstrip()) # 修剪左侧
print(email.rstrip()) # 修剪右侧
# 将指定字符串替换为新的内容
content = " 马化腾是个SB "
print(content.replace("马化腾", "*").replace("SB", "*"))
字符串的拆分和合并
content.split(”拆分依据”,maxsplit = “拆分次数”)
默认以空格拆分,可以指定逗号或其他拆分依据。
可以用maxsplit指定拆分次数
content = " I love you, you love me."
content = content.replace(",", "").replace(".", "")
print(content.split())
# ['I', 'love', 'you', 'you', 'love', 'me']
合并字符串,用指定字符连接
words = ["a", "b", "c", "d"]
print("#".join(words))
字符串的编码和解码
a = "我爱你"
b = a.encode("gbk")
print(type(b))
print(b, len(b))
print(b.decode("gbk"))
字符串的加密
content = "i love you."
table = str.maketrans(
"abcdefghijklmnopqrstuvwxyz",
"defghijklmnopqrstuvwxyzabc"
)
print(content.translate(table))
例子3
生成随机验证码
import random
import string
all_chars = string.ascii_lowercase + string.digits
for _ in range(10):
selected_chars = random.choices(all_chars, k=4)
print(selected_chars)