如何在 Python 3.x 中使用 string.replace()

2024-12-24 08:55:00
admin
原创
121
摘要:问题描述:在Python string.replace()3.x 上已弃用。 有什么新方法可以做到这一点?解决方案 1:与 2.x 一样,使用str.replace()。例子:>>> 'Hello world'.replace('world', 'Guido') 'Hello Guido' ...

问题描述:

在Python string.replace()3.x 上已弃用。 有什么新方法可以做到这一点?


解决方案 1:

与 2.x 一样,使用str.replace()

例子:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

解决方案 2:

replace()是python3中的一个方法<class 'str'>

>>> 'hello, world'.replace(',', ':')
'hello: world'

解决方案 3:

Python 3 中的 replace() 方法用法很简单:

a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))

#3 is the maximum replacement that can be done in the string#

>>> Thwas was the wasland of istanbul

# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached

解决方案 4:

您可以将str.replace()用作str.replace()。假设您有一个字符串'Testing PRI/Sec (#434242332;PP:432:133423846,335)',并且想要将所有'#',':',';','/'符号替换为'-'。您可以通过以下方式(正常方式)进行替换,

>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

或者这种方式( str.replace()链)

>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

解决方案 5:

str.replace官方文档Python 3

官方文档:Python 3str.replace

str.replace(旧的,新的[,计数])

返回字符串的副本,其中所有出现的子字符串 old 均被 new 替换。如果给出了可选参数 count,则仅替换前 count 个出现的位置。

对应VSCode的语法说明为:

在此处输入图片描述

str.replace(self:str,旧的,新的,计数) -> str

两种使用方法str.replace

  • 方法 1:使用内置 str 的 replace ->str.replace(strVariable, old, new[, count])

replacedStr1 = str.replace(originStr, "from", "to")
  • 方法2:使用str变量的替换->strVariable.replace(old, new[, count])

replacedStr2 = originStr.replace("from", "to")

完整演示

代码:

originStr = "Hello world"

# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))

# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))

输出:

case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li

截屏:

在此处输入图片描述

我的相关(中文)帖子:【详解】Python 3中字符串的替换str.replace

解决方案 6:

尝试一下:

mystring = "This Is A String"
print(mystring.replace("String","Text"))

解决方案 7:

为了提高可读性:将一些字符附加到位置固定的单词

例如:通过添加后缀-ly将形容词转换为副词。

为了提高可读性,您可以将后缀放在行尾以提高可读性。为此,请在split()inside使用replace()

s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'

解决方案 8:

简单替换: .replace(old, new, count) 。

text = "Apples taste Good."
print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
Bananas taste Good.          <---- Output

print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
Have a Good Day!             <----- Output

print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
Dad is angry!                <----- Output

解决方案 9:

ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用