Python常用字符串替换函数strip、replace及sub用法示例

作者:Together_CZ 时间:2022-04-07 18:21:17 

本文实例讲述了Python常用字符串替换函数strip、replace及sub用法。分享给大家供大家参考,具体如下:

今天在做一道今年秋季招聘题目的时候遇上了一个替换的问题,题目看起来好长好复杂啊,真的,一时间,我看了好几遍也没看懂,其实实质很简单,就是需要把给定的一个字符串里面的指定字符替换成一些指定的内容就行了,这样首选当然是字典了,没有之一,题目很简单就不写出来了,在这里花了一点时间专门总结了一下字符串的替换的几个常用的函数,希望也能帮到有需要的人,自己也是当做一个学习的记录,好了,在这里就不多说什么了,在代码中该说的都说了,直接看程序:


#!/usr/bin/env python
# coding:utf-8
import re
'''''
功能:对常见的几种字符串处理函数进行测试使用学习
Author:沂水寒城
'''
def str_test():
 str_list=['We are family!!!', '00 11 22 33 44 55 66 77 88 99',
      'Trouble is a friend!!!Trouble is a friend!!!', 'LoveLoveLove']
 str_dict={
   '!!!':'$$$',
   ' ':'@',
   'T':'t',
   'L':'&'
 }
 #使用replace
 '''''
 基本用法:对象.replace(rgExp,replaceText,max)
 rgExp和replaceText是必须要有的,max是可选的参数
 '''
 str_list1=str_list
 res_list=[]
 for one_str in str_list1:
   for key in str_dict:
     one_str = one_str.replace(key, str_dict[key])
   res_list.append(one_str)
 print '**************replace替换结果为:*********************'
 print str_list1
 print res_list
 #使用re
 '''''
 re.sub()有5个参数,三个必选参数pattern,repl,string;两个可选参数count,flags
 re.sub(pattern,repl,string,count,flags)
 pattern:表示正则表达式中的模式字符串;
 repl:被替换的字符串,或者是一个方法(既可以是字符串,也可以是函数);
 当repl为字符串的时候,也就是需要 将string中与pattern匹配的字符串都替换成repl
 当repl为方法的时候,就必须是一个带有一个参数,且参数为MatchObject类型的方法,该方法需要返回一个字符串。
 string:要被处理的,要被替换的字符串;
 count:指的是最大的可以被替换的匹配到的字符串的个数,默认为0,就是所有匹配到的字符串。
 flgas:标志位
 '''
 str_list2=str_list
 res_list=[]
 pattern_rule=re.compile(r'!!!')
 for one_str in str_list2:
   one_str = re.sub(pattern_rule, '$$$', one_str)
   res_list.append(one_str)
 print '**************sub替换结果为:*********************'
 print str_list2
 print res_list
 #使用strip()
 '''''
 个人使用strip()很久了,感觉这个函数在一些事比如字符串末尾换行符去除等方面出奇的好用,
 它并不算是一个纯正意义上跟上面两个函数类似的字符串处理的函数,但是用于字符串尾部删除等方面的时候
 效果还是很不错的
 '''
 str_list3=str_list
 res_list=[]
 for one_str in str_list3:
   one_str=one_str.strip('!!!')
   res_list.append(one_str)
 print '**************strip替换结果为:*********************'
 print str_list3
 print res_list
str_test()

结果如些下:

**************replace替换结果为:*********************
['We are family!!!', '00 11 22 33 44 55 66 77 88 99', 'Trouble is a friend!!!Trouble is a friend!!!', 'LoveLoveLove']
['We@are@family$$$', '00@11@22@33@44@55@66@77@88@99', 'trouble@is@a@friend$$$trouble@is@a@friend$$$', '&ove&ove&ove']
**************sub替换结果为:*********************
['We are family!!!', '00 11 22 33 44 55 66 77 88 99', 'Trouble is a friend!!!Trouble is a friend!!!', 'LoveLoveLove']
['We are family$$$', '00 11 22 33 44 55 66 77 88 99', 'Trouble is a friend$$$Trouble is a friend$$$', 'LoveLoveLove']
**************strip替换结果为:*********************
['We are family!!!', '00 11 22 33 44 55 66 77 88 99', 'Trouble is a friend!!!Trouble is a friend!!!', 'LoveLoveLove']
['We are family', '00 11 22 33 44 55 66 77 88 99', 'Trouble is a friend!!!Trouble is a friend', 'LoveLoveLove']

这些东西应该算得上是很顺手的小工具了,特别是在一些应用中能起到四两拨千斤的作用,也许是夸张了哈,但是就是很喜欢这几个小工具,所以就写出来分享一下,不足之处还望多多指教,大家共同学习共同进步!

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.csdn.net/together_cz/article/details/70172083

标签:Python,字符串,strip,replace,sub
0
投稿

猜你喜欢

  • 两侧背景自动延伸的CSS实现方法

    2010-02-24 09:42:00
  • sqlserver中如何查询出连续日期记录的代码

    2011-09-30 11:16:56
  • Go语言编程中对文件读写的基本方法整理

    2023-06-24 09:31:07
  • asp如何对用户进行授权?

    2009-11-20 18:46:00
  • 大容量SQL Server数据库迁移偏方

    2011-05-05 08:18:00
  • 网页标准化-CSS命名规划整理

    2007-12-10 18:13:00
  • Python os.access()用法实例

    2022-12-06 01:37:50
  • Python中pass的作用与使用教程

    2023-05-05 23:05:05
  • PHP入门速成(3)

    2023-11-20 18:48:03
  • 解决Dreamweaver不支持中文文件名

    2008-06-04 09:37:00
  • python装饰器相当于函数的调用方式

    2021-05-13 13:39:03
  • oracle怎样修改表名、列名、字段类型、添加表列、删除表列

    2010-07-23 11:10:00
  • The Story of Mr.Gray — Web 交互设计“灰色”的8类应用

    2009-12-30 16:57:00
  • 通过Python实现控制手机详解

    2021-04-21 12:10:37
  • php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法

    2023-07-16 11:36:01
  • Python3自定义http/https请求拦截mitmproxy脚本实例

    2021-04-13 15:33:01
  • mssql2005,2008导出数据字典实现方法

    2023-07-23 19:11:30
  • 细化解析:MySQL 数据库中对XA事务的限制

    2009-01-14 11:59:00
  • 如何提示用户打开Cookie?

    2010-06-07 20:53:00
  • 约瑟夫问题的Python和C++求解方法

    2023-12-05 15:09:28
  • asp之家 网络编程 m.aspxhome.com