string类型是不可变的,因此不能采用直接赋值的方式。比如一个字符串 helloworld,想把o替换成z,那么只有先替换,然后再迭代。

strings="helloworld"
hello=strings.replace('o','z')
for index,string in enumerate(hello):
print index,string

还有一种方法:

import string
table=string.maketrans('o','z')
for index,string in enumerate(strings.translate(table)):
print index,string

显示效果:

0 h
1 e
2 l
3 l
4 z
5 w
6 z
7 r
8 l
9 d

--End--

Python 替换字符串的更多相关文章

  1. Python替换字符串中的空格

    这是来源剑指offer的第二题,直接调用replace函数进行空格替换即可. 当我又想试试挨个字符比较进行替换时程序报错了: 错误原因是在python中字符串是一个不可变的数据类型,如果进行替换字符可 ...

  2. Python替换字符串中的反斜杠\

    s = 'cdp\nd' result = eval(repr(s).replace('\\', '@')) print(result) repr() 函数可以将字符串转换为python的原始字符串( ...

  3. python 替换字符串的方法replace()、正则re.sub()

    一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b   1 2 3 二.re ...

  4. python 替换指定目录下,所有文本字符串

    网页保存后,会把js文件起名为.下载,html里面的引用也会有,很不美观,解决方案:用python替换字符串 import os import re """将当前目录下所 ...

  5. Python - 安全替换字符串模板(safe_substitute) 详细解释

    安全替换字符串模板(safe_substitute) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 字 ...

  6. Python正则替换字符串函数re.sub用法示例(1)

    本文实例讲述了Python正则替换字符串函数re.sub用法.分享给大家供大家参考,具体如下: python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串然后把它替 ...

  7. python中的replace无法替换字符串

    replace替换字符串的时候,需要重新赋值给变量,因为在python中字符串是不可变对象,因此在使用的时候我们必须重新赋值,就这么简单. z1=z1.replace('ddd','')     返回 ...

  8. python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  9. Python之word文档替换字符串(也可以用于短模板套用)

    Python之word文档替换字符串(也可以用于短模板套用),代码如下: 1 ''' 2 #word模板套用1:创建模板,适合比较短的文档 3 ''' 4 5 #导入所需库 6 from docx i ...

随机推荐

  1. CSS基础(背景、文本、列表、表格、轮廓)

    CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...

  2. [转]Ubuntu下GitHub的使用

    转自Pythoner 本文将对Ubuntu下Git的安装,以及如何连接GitHub进行讲解. 1.环境 OS: Ubuntu13.04 64bitsGit: 1.8.1.2 2.Git安装 执行如下命 ...

  3. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  4. No module named BeautifulSoup

    遇到 No module named BeautifulSoup 错误,但是的确从官方下载了BeautifulSoup,并安装成功. 后来才发现,有两个BeautifulSoup的版本,一个是2012 ...

  5. DelphiXE7中创建WebService(服务端+客户端)

    相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...

  6. 开发WebForm时遇到的问题

    在做一个小项目时,一个很长的页面,页面底部有一个contact us form 整个页面我没有使用MVC,而是使用ASP.NET WebForm(.aspx)来实现,实现功能后发现,当用户在页面底部输 ...

  7. 网络子系统53_ip协议分片重组_内存阈值

    //调用路径:ip_defrag->ip_evictor // 分片重组时,可使用内存上下限: // 1.sysctl_ipfrag_high_thresh 可用内存上限 // 2.sysctl ...

  8. 网络子系统54_ip协议分片重组_定位ipq

    //为分片确定正确的ipq结构 // 定位5元组 // 1.<id, 源ip, 目的ip, l4协议> 可通过ip报文获取 // 2.user 通过ip_defrag给出,指出重组是由谁发 ...

  9. poj 1191 棋盘分割 动态规划

    棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11457   Accepted: 4032 Description ...

  10. Measuring Signal Similarities

    http://cn.mathworks.com/help/signal/examples/measuring-signal-similarities.html Open This Example   ...