Python 课程笔记

本文介绍了Python中字符串的各种操作,包括切片、查找、替换、分割等。通过实例展示了find方法的使用,以及rfind、count方法在查找子串中的应用。此外,还探讨了字符串的修改、join和split函数,以及capitalize方法的功能。这些基础知识对于理解和操作Python字符串至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

name ="hello python"
print(name[6])
### qiepian capzuo
name1 = "abcdefg"
print(name1[2:5:1])
print(name1[2:5])
print(name1[:5])
print(name1[1:])
print(name1[:])
print(name1[::2])
print(name1[:-1])#-1表示最后一个数
### 字符串find操作
myster = "hello word and hello python"
print(myster.find("hello"))
print(myster.find("hello",10,27))
print(myster.find("student"))
print(myster.find("hello"))
print(myster.find("hello",10,27))
print(myster.find("student"))
###   rfind
myster1 = "python.png"
print(myster1.rfind(".png"))
print(myster1.rfind(".png"))
print(myster1.rfind(".png"))
###   count
myster2 = "hello word and hello python hello web"
print(myster2.count("hello"))
###  count
myster2 = "hello word and hello python hello web"
print(myster2.count("h"))


###      字符串修改
myster3 = "hello word and hello python"
print(myster3.replace("and","&"))
print(myster3.replace("hello","hellow"))
###  按照指定字符分割字符串
myster4 = "apple,orange,banana"
print(myster4.split(","))
print(myster4.split(",",1))
print(myster4.split("*"))
###  join():用一个字符或字串合并字符串,并把
name2 = "hellow"
name3 = "world"
print(name2.join(name3))
###  将字符第一个字符大写,其他小写
myster5 = 'studenT'
print(myster5.capitalize() )
###   

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值