当前位置: 代码迷 >> 综合 >> Python 3 字符串 replace( ) 方法
  详细解决方案

Python 3 字符串 replace( ) 方法

热度:99   发布时间:2024-02-08 05:06:31.0

Python 3 字符串 replace( ) 方法


Python 3.8.5

语法格式:

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

描述:

  • 创建一个字符串,把原字符串中的旧字符或子字符串 old 替换成新字符或字符串 new。
  • 如果给出了可选参数 count,则只替换前 count 次出现。

参数说明:

  • old —— 将被替换的字符或子字符串。
  • new —— 新字符或字符串,用于替换旧的字符或子字符串。
  • count —— 可选参数,替换前 count 次出现。

返回值:

  • 替换后的新字符串。

示例:

>>> a = '路飞,路飞,你真丑!'
>>> a.replace('路飞', '山治')
'山治,山治,你真丑!'>>> a.replace('路飞', '山治', 1)
'山治,路飞,你真丑!'

 

  相关解决方案