
Python字符串切片、查找、修改、删除、判断等常用操作
2.1
2.2
2.3
2.4
3.1
3.2
3.3
3.4
3.5
3.6
3.7
5.1
5.2
5.3
5.4
5.5
5.6
发布日期:2021-05-15 00:32:58
浏览次数:23
分类:精选文章
本文共 3030 字,大约阅读时间需要 10 分钟。
Python 字符串操作指南
1. 切片
切片是对字符串操作对象截取一部分的操作,适用于字符串、列表和元组等可切片的对象。
1.1 字符串切片
name = "abcdefg"print(name[2:5]) # 输出: "cde"print(name[:5]) # 输出: "abcde"print(name[1:]) # 输出: "bcdefg"print(name[::-1]) # 输出: "gfedcba"
1.2 逆序字符串
name = "abcdefg"print(name[::-1]) # 输出: "gfedcba"
2. 查找
2.1 find()
方法
检测子串是否包含在字符串中,返回子串起始位置或 -1。
mystr = "hello world and itcast and itheima and Python"print(mystr.find('and')) # 输出: 12print(mystr.find('ands')) # 输出: -1
2.2 index()
方法
检测子串是否存在,存在则返回起始位置,不存在则报异常。
mystr = "hello world and itcast and itheima and Python"print(mystr.index('and')) # 输出: 12
2.3 count()
方法
返回子串在字符串中出现的次数。
mystr = "hello world and itcast and itheima and Python"print(mystr.count('and')) # 输出: 3
2.4 rfind()
和 rindex()
从字符串末尾开始查找子串。
mystr = "hello world and itcast and itheima and Python"print(mystr.rfind('and')) # 输出: 23
3. 修改
3.1 replace()
方法
替换字符串中的子串。
mystr = "hello world and itcast and itheima and Python"print(mystr.replace('and', 'he')) # 输出: "hello world he itcast he itheima he Python"
3.2 split()
方法
分割字符串,返回列表。
mystr = "hello world and itcast and itheima and Python"print(mystr.split('and')) # 输出: ['hello world ', ' itcast ', ' itheima ', ' Python']
3.3 join()
方法
合并多个字符串。
list1 = ['chuan', 'zhi', 'bo', 'ke']print('_'.join(list1)) # 输出: "chuan_zhi_bo_ke"
3.4 capitalize()
方法
将字符串的第一个字符转换为大写,其余字符转换为小写。
mystr = "hello world and itcast and itheima and Python"print(mystr.capitalize()) # 输出: "Hello world and itcast and itheima and python"
3.5 title()
方法
将字符串中的每个单词首字母转换为大写。
mystr = "hello world and itcast and itheima and Python"print(mystr.title()) # 输出: "Hello World And Itcast And Itheima And Python"
3.6 lower()
方法
将字符串中的大写字符转换为小写。
mystr = "hello world and itcast and itheima and Python"print(mystr.lower()) # 输出: "hello world and itcast and itheima and python"
3.7 upper()
方法
将字符串中的小写字符转换为大写。
mystr = "hello world and itcast and itheima and Python"print(mystr.upper()) # 输出: "HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON"
4. 删除
4.1 删除空白字符
mystr = " hello world "print(mystr.lstrip()) # 输出: "hello world "print(mystr.rstrip()) # 输出: " hello world"print(mystr.strip()) # 输出: "hello world"
4.2 字符串对齐
mystr = "hello world"print(mystr.ljust(20)) # 输出: "hello world "print(mystr.rjust(20)) # 输出: " hello world"print(mystr.center(20)) # 输出: " hello world "
5. 判断
5.1 startswith()
方法
检查字符串是否以指定子串开头。
mystr = "hello world and itcast and itheima and Python"print(mystr.startswith('hello')) # 输出: True
5.2 endswith()
方法
检查字符串是否以指定子串结尾。
mystr = "hello world and itcast and itheima and Python"print(mystr.endswith('Python')) # 输出: True
5.3 isalpha()
方法
检查字符串是否全为字母。
mystr1 = 'hello' # 输出: Truemystr2 = 'hello12345' # 输出: False
5.4 isdigit()
方法
检查字符串是否全为数字。
mystr1 = 'aaa12345' # 输出: Falsemystr2 = '12345' # 输出: True
5.5 isalnum()
方法
检查字符串是否全为字母或数字。
mystr1 = 'aaa12345' # 输出: Truemystr2 = '12345-' # 输出: False
5.6 isspace()
方法
检查字符串是否全为空白字符。
mystr1 = '1 2 3 4 5' # 输出: Falsemystr2 = ' ' # 输出: True
以上方法是对字符串操作的基础实现,熟练掌握这些方法能够显著提升代码效率和代码质量。
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年05月02日 03时55分51秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
grafana改用https登录
2019-03-12
使用jenkins进行项目的自动构建部署
2019-03-12
使用MySQLTuner-perl对MySQL进行优化
2019-03-12
2018年3月最新的Ubuntu 16.04.4漏洞提权代码
2019-03-12
异或交换两个数的值
2019-03-12
使用python绘出常见函数
2019-03-12
Golang AES加密
2019-03-12
Puppet的一些奇技淫巧
2019-03-12
foreman源NO_PUBKEY 6F8600B9563278F6
2019-03-12
亚马逊aws文档语法错误
2019-03-12
什么是5G?居然有人用漫画把它讲得如此接地气!
2019-03-12
Spring cloud --分布式配置中心组件Spring Cloud Config
2019-03-12
UE4接入Android第三方库2——通过JIN与GameActivity通信
2019-03-12
Unity Job System 2——并行处理数据
2019-03-12
BIG解决保险欺诈问题,开创数字化保险时代
2019-03-12
Apache JMeter5.3 压力测试
2019-03-12
maven打包多环境配置
2019-03-12
c++ hpp使用好处
2019-03-12
Mac 使用Eclipse老是闪退解决方案
2019-03-12
谈笑间学会-Hbase Rowkey设计
2019-03-12