python字符串replace()方法
2016-07-16 11:10
711 查看
>>> 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!'
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!'
相关文章推荐
- 关于Python2中utf8,gbk,ascii的编码问题
- 深入Python(4):深拷贝和浅拷贝
- 关于Python的若干常用片段
- Python json和pickle模块
- Python模拟用户提交订单
- BosonNLP试用
- Web.py - NotFound和InternalError
- Python新手学习基础之数据类型——字符串类型
- 文成小盆友python-num10 socketserver 原理相关。
- 编程语言学习——python(1)
- python - socket模块1
- Python中的编码问题
- Python多进程写入同一文件
- python爬虫抓取游民星空每日囧图
- python set集合运算(交集,并集,差集,对称差集)
- 使用python构建污染溯源系统
- SCU2016-06 P题 python处理字符串
- python-socket 粘包问题
- Python 安装第三方模块
- python之路 IO多路复用 线程进程初步了解