Python实训--语法基础三

⽬标

字符串

认识字符串

字符串是 Python 中最常⽤的数据类型。我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为 变量分配⼀个值即可。

下标

“下标” ⼜叫 “索引” ,就是编号。⽐如⽕⻋座位号,座位号的作⽤:按照编号快速找到对应的座位。同 理,下标的作⽤即是通过下标快速找到对应的数据。

切⽚

切⽚是指对操作的对象截取其中⼀部分的操作。字符串、列表、元组都⽀持切⽚操作。

常⽤操作⽅法

字符串的常⽤操作⽅法有查找、修改和判断三⼤类。

查找

所谓字符串查找⽅法即是查找⼦串在字符串中的位置或出现的次数。

find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返 回-1。

语法:字符串序列.find(⼦串, 开始位置下标, 结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。

实例:

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1
index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则 报异常。

语法:字符串序列.index(⼦串, 开始位置下标, 结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。

实例:

mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
rfind(): 和find()功能相同,但查找⽅向为右侧开始。
rindex():和index()功能相同,但查找⽅向为右侧开始。
count():返回某个⼦串在字符串中出现的次数

语法:字符串序列.count(⼦串, 开始位置下标, 结束位置下标)

实例:

mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1
修改

所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。

replace():替换

语法:字符串序列.replace(旧⼦串, 新⼦串, 替换次数)

注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)

注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候 不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

split():按照指定字符分割字符串。

语法:字符串序列.split(分割字符, num)

注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))

注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串。

join():⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串。

语法:字符或⼦串.join(多字符串组成的序列)

实例:

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))
capitalize():将字符串第⼀个字符转换成⼤写。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())

注意:capitalize()函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写。

title():将字符串每个单词⾸字⺟转换成⼤写。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())
lower():将字符串中⼤写转⼩写。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())
upper():将字符串中⼩写转⼤写。

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())
判断
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。

startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开 始和结束位置下标,则在指定范围内检查。 

语法:字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)

实例:

mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开 始和结束位置下标,则在指定范围内检查。

语法:字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)

实例:

mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))

总结

下标

计算机为数据序列中每个元素分配的从0开始的编号

切⽚

序列名[开始位置下标:结束位置下标:步⻓]

常⽤操作⽅法

find()

index()

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AustSimple

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值