【说站】python删除str中特定字符的方法
2024-12-31
33
python删除str中特定字符的方法
1、删除字符串首尾的多余字符串strip()
# 删除字符串中多余字符 def string_remove(): str1 = ' abc \n' print str1.strip() # abc str2 = '----abcdf++++' print str2.strip('-+') # abcdf
2、replace函数,删除字符串中某一个所有的字符串
ss = 'old old string' ret = ss.replace('old', 'new', 1) print(ret)
3、sub函数,同时删除多个字符串,使用正则表达式
str2 = '\nabc\nwrt22\t666\t' # 删除字符串中的所有\n,\t import re print(re.sub('[\n\t]','',str2)) # abcwrt22666
以上就是python删除str中特定字符的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
更新于:16天前赞一波!
相关文章
- 【说站】mysql有哪些建立索引的方法
- 【说站】mysql表导出的两种方法
- 【说站】python Pandas读取数据文件的优点
- 【说站】python中in和is的区分
- 【说站】python异常中常见关键字
- 【说站】python os.path.join()函数的使用
- 【说站】python如何使用skimage包提取图像
- 【说站】python confusion_matrix()是什么
- 【说站】python中os.path.join()函数是什么
- 【说站】python中有哪些比较操作
- 【说站】python字符串的用法总结
- 【说站】php方法断点如何实现
- 【说站】java类的两种引用方法
- 【说站】python列表数据如何增加和删除
- 【说站】java转义字符
- 【说站】python解释器的多种使用
- 【说站】python多行注释的方法整理
- Google翻译退出中国:带访问方法(已验证)
- 【说站】python列表有哪些特点
- 【说站】Python继承的原理分析
文章评论
评论问答