Python - 替换字符串中的尾部单词

pythonserver side programmingprogramming更新于 2023/8/31 16:46:00

在本文中,我们将学习如何用任何其他给定单词替换尾部单词(字符串末尾的单词)。您可能在操作字符串时见过这个问题。在这里,我们将看到用给定单词替换字符串中后缀单词的各种方法。

让我们使用下面的示例来了解这一点 -

string_txt = "This cat is running very Fast"

这里我们有一个包含句子的字符串 string_txt,我们想将最后一个单词"Fast"替换为"Slow",以便生成的字符串如下所示。

string_txt = "This cat is running very Slow"

执行此操作的方法。

方法 1. 使用 Split() 和 Join() 函数。

示例

def replace_rear(string_txt, new_str):
   words = string_txt.split()
   words[-1] = new_str
   return ' '.join(words)
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用 split() 方法将字符串拆分成单词列表。我们使用 words[-1] 访问最后一个单词,并使用 new_str 替换这个单词,new_str 是我们想要用最后一个单词替换的新单词。然后我们使用 join() 方法将单词列表重新组合成字符串。

方法 2. 使用正则表达式。

示例

import re

def replace_rear(string_txt, new_str):
   return re.sub(r'\b\w+\b$', new_str, string_txt)
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用正则表达式中的 re.sub() 方法执行正则表达式替换。我们定义了一个模式 r'\b\w+\b$',它与字符串的最后一个单词匹配,并将其替换为 new_str。

方法 3. 使用 Rsplit() 函数。

示例

import re

def replace_rear(string_txt, new_str):
   words = string_txt.rsplit(None, 1)
   return words[0] + ' ' + new_str
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用了 rsplit() 方法,该方法允许我们从最右侧拆分字符串。因此,我们使用该方法从右侧开始将字符串拆分为单词。我们将分隔符定义为 None,将拆分次数定义为 1,因为我们只想替换最后一个单词。然后我们将拆分的字符串与 new_str 组合。因此,它最终将用给定的新单词替换该单词。

方法 4. 使用列表推导。

示例

import re

def replace_rear(string_txt, new_str):
   words = string_txt.split()
   words = [new_str if i == len(words) - 1 else word for i, word in enumerate(words)]
   return ' '.join(words)
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用列表推导来遍历单词,并在到达最后一个索引 len(words)-1 时替换最后一个单词。然后我们使用 join() 函数将拆分的字符串重新组合。因此,我们的新字符串在末尾包含替换的单词。

方法 5. 使用字符串切片技术。

示例

import re

def replace_rear(string_txt, new_str):
   last_space_index = string_txt.rindex(' ')
   return string_txt[:last_space_index+1] + new_str
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用字符串切片技术将字符串的最后一个单词替换为新单词。我们首先使用 rindex() 找到字符串的最后一个空格,然后使用切片将字符串拆分到最后一个索引,然后将其与空格组合并附加我们想要替换的新单词。

方法 6. 使用 Split()、Maxsplit 和 Join() 方法。

示例

import re

def replace_rear(string_txt, new_str):
   words = string_txt.split(maxsplit=string_txt.count(' ') or None)
   words[-1] = new_str
   return ' '.join(words)
   
string_txt = "This cat is running very Fast"
print("String before replace: ", string_txt)
new_string = replace_rear(string_txt, "Slow")
print("String after replacing rear: ", new_string)

输出

String before replace: This cat is running very Fast 
String after replacing rear: This cat is running very Slow

解释

在上面的例子中,我们使用 maxsplit=string_txt.count('') 或 None 来指定字符串拆分将发生在最后一个空格处。然后在拆分最后一个单词后,我们用 new_str 替换最后一个单词,并使用 join() 函数将拆分后的字符串重新组合起来

因此,我们了解了用新单词替换字符串最后一个单词的方法。我们看到了解决问题的各种方法,包括正则表达式、列表推导、split() 和 rsplit() 函数。每种方法都有其独特的解决问题的方法,您可以根据需要选择任何合适且简单的方法。


相关文章