1 字符串
格式化字符串
name = "张三"
age = 19
# 三种格式化方法
str1 = "我叫{},我{}岁".format(name, age)
str2 = "我叫%s,我%d岁"%(name, age) # %d:十进制整数
str3 = f"我叫{name},我{age}岁"
print(str3)
2 下标(索引)
name = "小明很忙"
for i in name:
print(i) # print()默认换行
for i in name:
print(i, end="") # 不换行
print(name, end="+")
下标和索引从0开始,根据下标和索引进行访问。
name = "小明很忙"
print(name[0]) # 小
print(name[1]) # 明
print(name[2]) # 很
print(name[3]) # 忙
li_list = ["张三", "李四", "王五"]
print(li_list[0])
print(li_list[1])
print(li_list[2])
切片
只取字符串和列表的一部分
str1 = "hello, world" # 中间有个空格
# h e l l o , 空格 w o r l d
# 0 1 2 3 4 5 6 7 8 9 10 11
print(str1[0:4]) # hell
print(str1[0:5]) # hello
print(str1[0:]) # hello, world
print(str1[1:]) # ello, world
print(str1[7:]) # world
print(str1[7:11]) # worl
print(str1[7:12]) # world
print(str1[:]) # hello, world
# 加上步长
print(str1[::2]) # hlo ol
# 反过来
print(str1[::-1]) # dlrow ,olleh
提问:如何输出”lo olh“
str1 = "hello, world"
print(str1[-2::-2])
3 字符串 常用操作方法
3.1 查找
3.1.1 find()方法
检测某个元素是否在字符串中,有就返回开始的下标,没有就返回-1
str1 = "hello, world"
str2 = "ll"
print(str1.find(str2)) # 在str1中寻找有没有str2,返回:2
str3 = "lll"
print(str1.find(str3)) # 返回:-1
3.1.2 index()方法
检测某个元素是否在字符串中,有就返回开始的下标,没有就报错
index找不到会报错,find会返回-1
列表里面也有index()方法,过后再说
3.1.3 count()方法
查看某个字符在字符串中出现的次数
str1 = "hello, world"
str2 = "l"
print(str1.count(str2)) # . 就代表调用的意思。返回:3
“. ” 代表调用
3.1.4 拓展 rfind() rindex()
var = "hello and python and hello world"
# rfind(): 和find()功能相同,但查找⽅向为右侧开始。
print(var.rfind("and"))
# rindex():和index()功能相同,但查找⽅向为右侧开始。
print(var.rindex("and"))
# 返回:
# 17
# 17
# rfind()返回最后一次出现的索引,find()返回的是第一次出现的索引,没有匹配的都是返回-1
3.2 修改
3.2.1 replace()方法
替换内容
str1 = "hello, world"
print(str1.replace("hello","hi")) # 返回:hi, world
注意:替换次数如果超出⼦串出现次数,则替换次数为该⼦串出现次数。
# 字符串序列.replace(旧⼦符, 新⼦符, 替换次数)
var = "hello and python and hello world"
print(var.replace("and","和")) # 将里面所有的and替换为和
print(var.replace("and","和",1)) # 将and替换为和,只替换一次
# 运行结果:
# hello 和 python 和 hello world
# hello 和 python and hello world
注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。
不可变数据类型更改后内存地址会发生改变,可变数据类型更改后内存地址不会发生改变。
3.2.2 split()方法
字符串的分割,指定一个分割符,按照指定方法分割
注意:num表示的是分割字符出现的次数
切割完成之后他会返回一个列表
# 字符串序列.split(分割字符, num)
var = "hello and python and hello world"
print(var.split("and")) # 以and为界,分隔开其他字符串,返回一个列表
print(var.split("and",1)) # 以and为界,分隔开其他字符串,只分割一次,返回一个列表
# 运行结果:
# ['hello ', ' python ', ' hello world']
# ['hello ', ' python and hello world']
这些方法不用背,要用的时候直接查,Python有什么方法能做到,知道几个主要的方法即可。
3.2.3 join()方法
几个字符合并成一个新的字符串
# 字符.join(多字符串组成的序列)
list1 = ["hello", "python", "i", "love", "you"] # 列表
t1 = ("hello", "python", "i", "love", "you") # 元组
set1 = {"hello", "python", "i", "love", "you"} # 集合
print("__".join(list1)) # 将列表转化为字符串,并且使用指定符号隔开
print(",".join(t1)) # 将元组转化为字符串,并且使用指定符号隔开
print("|".join(set1)) # 将集合转化为字符串,并且使用指定符号隔开
# 运行结果:
# hello__python__i__love__you
# hello,python,i,love,you
# i|python|you|love|hello
字符串和字符串之间只能使用加号,不能使用减号
字符串相乘
列表相加
print(["s"]+["i"]) # 返回:['s', 'i']
3.2.4 大小写转换
3.2.4.1 capitalize()
将字符串第⼀个字符转换成⼤写。
var = "hello and python and hello world"
print(var.capitalize()) # 将字符串第⼀个字符转换成⼤写。
# 运行结果:
# Hello and python and hello world
3.2.4.2 title()
将字符串每个单词⾸字⺟转换成⼤写。
var = "hello and python and hello world"
print(var.title()) # 将字符串每个单词⾸字⺟转换成⼤写。
# 运行结果:
# Hello And Python And Hello World
3.2.4.3 upper()
将字符串中⼩写转⼤写。
var = "hello and python and hello world"
print(var.upper()) # 将字符串中⼩写转⼤写。
# 运行结果:
# HELLO AND PYTHON AND HELLO WORLD
3.2.4.4 lower()
将字符串中⼤写转⼩写。
var = "hello and python and hello world,HELLO AND PYTHON AND HELLO WORLD"
print(var.lower()) # 将字符串中⼤写转⼩写。
# 运行结果:
# hello and python and hello world,hello and python and hello world
3.2.4.5 lstrip()
删除字符串左侧空⽩字符。 了解即可
var = " hello and python and hello world "
print(var.lstrip()) # 删除左侧空格
# 运行结果:
# hello and python and hello world (空格到这里)
3.2.4.6 rstrip()
删除字符串右侧空⽩字符。 了解即可
var = " hello and python and hello world "
print(var.rstrip()) # 删除右侧空格
# 运行结果:
# hello and python and hello world(右边没有空格)
3.2.4.7 strip()
删除字符串两侧空⽩字符。
var = " hello and python and hello world "
print(var.strip()) # 删除两侧空格
# 运行结果:
# hello and python and hello world(右边没空格)
3.2.4.8 just()
填充字符串
# 字符串序列.ljust(⻓度, 填充字符)
var = "hello"
print(var.ljust(10,"_")) # 左对齐
print(var.rjust(10,"_")) # 右对齐
print(var.center(10,"_")) # 居中对齐
# 运行结果:
# hello_____
# _____hello
# __hello___
3.3 判断
3.3.1 startswith()
检查字符串是否是以指定元素开头
# 字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)
var = "hello and python and hello world"
print(var.startswith("hello")) # 开头是hello,返回True
print(var.startswith("and")) # 开头不是and,返回False
print(var.startswith("and",6,20)) # 在索引6-20,开头是and,返回True
# 运行结果:
# True
# False
# True
3.3.2 endswith()
检查字符串是否是以指定元素结尾
var = "hello and python and hello world"
print(var.endswith("and")) # 结尾不是and,返回False
print(var.endswith("world")) # 结尾是world,返回True
print(var.endswith("and",0,9)) # 在0到9的索引范围,是and结尾,返回True
# 运行结果:
# False
# True
# True(0是h,9是第二个空格)
3.3.3 isalpha()
如果字符串所有字符都是字⺟则返回 True, 否则返回 False。
mystr1 = 'hello'
mystr2 = 'hello12345'
print(mystr1.isalpha()) # 结果:True
print(mystr2.isalpha()) # 结果:False
3.3.4 isdigit()
如果字符串只包含数字则返回 True 否则返回 False。
mystr1 = 'aaa12345'
mystr2 = '12345'
print(mystr1.isdigit()) # 结果: False
print(mystr2.isdigit()) # 结果:True
3.3.5 isalnum()
如果字符串所有字符都是字⺟或数字则返 回 True,否则返回False。
var1 = 'aaa12345'
var2 = '12345-'
print(var1.isalnum()) # 结果:True
print(var2.isalnum()) # 结果:False
3.3.6 isspace()
如果字符串中只包含空⽩,则返回 True,否则返回 False。
mystr1 = '1 2 3 4 5'
mystr2 = ' '
print(mystr1.isspace()) # 结果:False
print(mystr2.isspace()) # 结果:True
4 字符串运算
a = “Hello”,b = “Python”
+ | 字符串连接 | >>>a + b | ‘HelloPython’ |
---|---|---|---|
[] | 通过索引获取字符串中字符 | >>>a[1] | ‘e’ |
[ : ] | 截取字符串中的一部分 | >>>a[1:4] | ‘ell’ |
in | 成员运算符 - 如果字符串中包含给定的字符返回 True | >>>“H” in a | True |
not in | 成员运算符 - 如果字符串中不包含给定的字符返回 True | >>>“M” not in a | True |
r | 取消转义 | >>>r“你\n好” | 你\n好 |
% | 格式字符串 |
print("hello \nworld")
print(r"hello \nworld") # r 输出原始字符串
# 运行结果:
# hello
# world
# hello \nworld
5 day 07 作业
# 字符串操作
# 将字符串var1 = "1a2b3c4d5e6f"中的数字提取,并且累加