>>> help(str.replace)
Help on method_descriptor:
replace(...)
    S.replace(old, new[, count]) -> string
    
    Return a copy of string S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
>>> s='hello python,hello world,hello c++,hello java!'
>>> s.replace('hello','Hello')#将字符串s中的所有'hello'子串,替换成'Hello',返回替换后的字符串,原字符串s不变
'Hello python,Hello world,Hello c++,Hello java!'
>>> s
'hello python,hello world,hello c++,hello java!'
>>> s.replace('hello','Hello',2)#将字符串s中的前2个'hello'子串,替换成'Hello'
'Hello python,Hello world,hello c++,hello java!'
>>> s
'hello python,hello world,hello c++,hello java!'
>>> s.replace('wahaha','haha')#要替换的'wahaha'子串不存在,直接返回原字符串
'hello python,hello world,hello c++,hello java!'

python字符串replace()方法的更多相关文章

  1. python字符串的方法

    python字符串的方法 ############7个基本方法############ 1:join def join(self, ab=None, pq=None, rs=None): # real ...

  2. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  3. python字符串replace失效问题

    python字符串replace替换无效 背景 今天想把一个列表中符合条件的元素中 替换部分字符串, 发现怎么替换,改元素还是没有改变,本以为是内存引用的问题后来发现并不然. 经查阅解决 在Pytho ...

  4. 7. python 字符串格式化方法(2)

    7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...

  5. 7. python 字符串格式化方法(1)

    7. python 字符串格式化方法(1) 承接上一章节,我们这一节来说说字符串格式化的另一种方法,就是调用format() >>> template='{0},{1} and {2 ...

  6. Python字符串解析方法汇总

    Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: ...

  7. python字符串处理方法

    一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...

  8. python字符串使用方法归纳

    字符串的意思就是“一串字符”,比如“Hello,Charlie”是一个字符串,“How are you?”也是一个字符串. Python 要求字符串必须使用引号括起来,使用单引号也行,使用双引号也行, ...

  9. Python之replace()方法失效

    1.背景 Titanic存活率预测案例: # 读取数据 df_train = pd.read_csv("./data/train.csv") df_train.head() OUT ...

随机推荐

  1. Ubuntu使用Xming和Putty

    运行个复杂的,比如 gnome-session,这个是 GNOME 的启动命令,如果想打开 KDE 就是 startkde root@carlo-cloud:~# xlogoThe program ' ...

  2. Discuz!源代码阅读笔记之common.inc.php文件【1】

    <?php /* [Discuz!] (C)2001-2007 Comsenz Inc. This is NOT a freeware, use is subject to license te ...

  3. 七天学会SALTSTACK自动化运维 (3)

    七天学会SALTSTACK自动化运维 (3) 导读 SLS TOP.SLS MINION选择器 SLS文件的编译 总结 参考链接 导读 SLS SLS (aka SaLt State file) 是 ...

  4. python学习之 -mysql 连接和db_config配置

    最近学习python,记录下自己写学习python的代码和心得,自己写了一个使用python mysql 的查询语句和做的一个db_config.py 配置信息. 1.db_config.py 配置文 ...

  5. WCF返回JSON的详细配置

    开发环境:VS2008,c# 1.新建个WCF服务网站 文件-新建-网站-WCF服务 2,运行一下,提示配置WEB.CONFIG,点击确认. 3,打开web.config增加如下节点: <ser ...

  6. emmet插件的导入与实用

    http://jingyan.baidu.com/article/ff4116259b057c12e48237b8.html http://www.iteye.com/news/27580 分享htm ...

  7. 【Java】WebService教程

    Web Services Web Services可以将应用程序转换为网络应用程序. Web Services可以被其他应用程序利用. 基本的Web Services平台是XML + HTTP. WS ...

  8. C语言中如何使用宏

    C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结. 宏使用中的常见的基础问题  #符号和##符号的使用  ...符号的 ...

  9. COJ 0967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  10. CH Round #51 - Shinrein祭 #1

    A.Phorni 题目:http://www.contesthunter.org/contest/CH%20Round%20%2351%20-%20Shinrein祭%20%231/Phorni 没做 ...