导语:
本文主要介绍了关于python删除str中特定字符的方法的相关知识,包括python查找字符串中某个字符,以及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
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python中numpy.empty()函数的使用09/08
- ♥ python字典get()实例教程详解10/15
- ♥ 如何使python for循环不停止10/23
- ♥ 哪个更适合 Web 开发,python 还是 php?10/24
- ♥ python偏函数的使用01/11
- ♥ python是如何分布式存储文件的?12/08
内容反馈