描述

Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

语法

replace()方法语法:

str.replace(old, new[, max])

参数

old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次

返回值

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。

实例

以下实例展示了replace()函数的使用方法:

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

以上实例输出结果如下:

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Python replace()方法的更多相关文章

  1. Python replace方法并不改变原字符串

    直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...

  2. Python replace方法的使用

    在Python str 中, 有一个很方便的查找替换的函数 replace() my_str = "lowmanmana" new_str = my_str.replace(&qu ...

  3. 20190118-自定义实现replace方法

    1.自定义实现replace方法 Python replace() 方法把字符串中的 old(旧字符串) 替换成 neange(新字符串),如果指定第三个参数max,则替换不超过 max 次.考虑ol ...

  4. python字符串replace()方法

    python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...)    S.repla ...

  5. Python string replace 方法

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

  6. python中的replace()方法的使用

    python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...

  7. python字符串方法replace()简介

    今天写replace方法的时候的代码如下: message = "I really like dogs" message.replace('dog','cat') print(me ...

  8. Python之replace()方法失效

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

  9. Python中的replace方法

    replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...

随机推荐

  1. Unity Socket TCP

    using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Net.Sock ...

  2. OpenCV——彩色图像转成灰度图像

    // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include <iostr ...

  3. jquery 加法 乘法运算 精确计算函数

    int类型相加不会出现问题,但小数点相加就会出现问题 //乘法函数 var accMul = function(arg1, arg2){ var m=0,s1=arg1.toString(),s2=a ...

  4. spiral matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  5. Python用pip安装IPython/Jupyter最佳交互环境

    一.Python模块及安装包简介 如果说编程语言是武器,那么Python就是一把双管枪(Python2/Python3),而各种为Python编写的模块和包就是子弹.使用pip来填满我们的武器吧! I ...

  6. git如何移除某文件夹的版本控制

    目录结构如下 project bin lib src ...... 执行如下的操作 git add . git commit -m "add bin/ lib/ src/" git ...

  7. TypeError: can't compare offset-naive and offset-aware datetimes bugfix

    参考:https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#naive-and-aware-datetime-objects 起因: ...

  8. 一个简单的小小记账本程序(java)

    感觉基础知识学了不少,但是一直搞不清一个项目的实际开发流程,所以就借着这个小记账本的程序梳理一下.因为楼主也是出于学习阶段的菜鸟,所以程序可能会有各种玄学的bug,希望一起提升吧. 跟着站长学到了很多 ...

  9. printf("Hello 2018!");

    月考 has Boom! 要全心准备期末考试,到年前是不能再看Blog了 新年加油!!! 不要感冒 :joy:

  10. c++11线程池

    #pragma once #include <future> #include <vector> #include <atomic> #include <qu ...