zoukankan      html  css  js  c++  java
  • python字符串replace()方法

    python字符串replace()方法

    >>> 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!'
  • 相关阅读:
    Poj3126
    Poj1426
    2806 红与黑
    3100 蜗牛
    1225 八数码难题
    2549 自然数和分解
    2547 东方辉针城
    2928 你缺什么
    1629 01迷宫
    1029 遍历问题
  • 原文地址:https://www.cnblogs.com/weiman3389/p/6047067.html
Copyright © 2011-2022 走看看