栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python strip()问题记录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python strip()问题记录

一.
course_id = "AC2970Ta "
title_code = "ACBCT 2970 Taxes and 7usiness Strategy"
title_code

运行结果:

'ACBCTxa02970 Taxes and 7usiness Strategy'
course = title_code.strip(course_id)
course

运行结果:

'BCTxa02970 Taxes and 7usiness Strategy'
二.
course_id = "ACBCT 2970 "
title_code = "ACBCT 2970 Taxes and 7usiness Strategy"
course = title_code.strip(course_id)
course

运行结果:

'axes and 7usiness Strategy'
三.
course_id = "ACBa "
title_code = "ABCaT 2970 Taxes and 7usiness Strategy"
course = title_code.strip(course_id)
course

运行结果:

'Txa02970 Taxes and 7usiness Strategy'
四.
course_id = "ACBCT 2970 "
title_code = "ACBCT 2970 Taxes and 7usiness Strategy 2829ABC"
course = title_code.strip(course_id)
course

运行结果:

'axes and 7usiness Strategy 28'
理解(以三为例):

1, 先把course_id 变成cour=[”A“,“C”,“B”,“a”]
2, 然后对 title_code 元素 X 进行从左(头)往右和从右(尾)往左依次对比cour中的元素 Y,如果X==Y,则删掉X,直到X!=Y为止,返回title_code 剩下的子字符串的copy(因为python的字符串是不可改变的)


正则
import re
course_id = "ACB "
title_code = "ACB Taxes  ACB and S ACB trategy"
course = re.sub(course_id,"",title_code,2)
course

运行结果:

'Taxes  and S ACB trategy'
PS:数字二是从左往右删除的次数

如果只想删除开头的:

import re
course_id = "ACB "
title_code = "ACB Taxes  ACB and S ACB trategy"
course = re.sub("^"+course_id,"",title_code)
course

或者:

course = re.sub(course_id,"",title_code,1)
course

运行结果:

'Taxes  ACB and S ACB trategy'
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号