Python 从字符串末尾删除给定的子字符串

Python 从字符串末尾删除给定的子字符串

Python是全球使用的编程语言,被开发人员用于不同的目的。Python有各种不同的应用,例如Web开发、数据科学、机器学习,以及执行自动化的不同过程。所有使用python的不同程序员都必须处理字符串和子字符串。所以在本文中,我们将学习如何删除字符串末尾的子字符串。

不同的方法来删除子字符串

使用函数

我们将使用endswith()函数,它帮助我们删除字符串末尾的子字符串。为了更清楚地理解,我们将举以下示例:

示例

def remove_substring(string, substring):  #Defining two different parameters
    if string.endswith(substring):
        return string[:len(string)-len(substring)]   #If substring is present at the end of the string then the length of the substring is removed from the string
    else:
        return string   #If there is no substring at the end, it will return with the same length

# Example 
text = "Hello Everyone, I am John, The Sailor!"
last_substring = ", The Sailor!" #Specifying the substring
#Do not forget to enter the last exclamation mark, not entering the punctuations might lead to error
Without_substring = remove_substring(text, last_substring)
print(Without_substring)

输出

上述代码的输出将如下所示:

Hello Everyone, I am John

切割字符串

在这种方法中,我们将切割字符串末尾的子字符串。Python提供了在代码中切割文本或字符串的功能。我们将在程序中定义子字符串并相应地进行切割。使用此方法删除子字符串的代码示例如下:

示例

def remove_substring(string, substring):
    if string[-len(substring):] == substring:  #The length of last characters of the string (length of substring) is compared with the substring and if they are same the substring is removed 
        return string[:-len(substring)]  
    else:
        return string  #If the length of last characters(Substring) does not match with the length of last substring then the characters are not removed 

# Example 
Whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
Final_String = remove_substring(Whole_string, last_substring)
print(Final_String)

输出

上述代码的输出如下:

Hello Everyone, I am John

re模块

re模块是Python编程语言中的一个模块,用于处理正则函数。我们可以使用re模块的一个函数来删除字符串末尾的子字符串。我们将使用的函数是re.sub()函数。使用re模块函数删除字符串的最后一个子字符串的代码和示例如下:

示例

import re  #Do not forget to import re module or it might lead to error while running the program

def remove_substring(string, substring):
    pattern = re.escape(substring) + r''  #re.escape is used to create a pattern to treat all symbols equally and it includes to work only on the substring on the end of the string
    return re.sub(pattern, '', string)  #This replaces the last substring with an empty space

# Example
Whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
Final_String = remove_substring(Whole_string, last_substring)
print(Final_String)

输出

上述代码的输出结果如下:

Hello Everyone, I am John

字符串切片的同时使用函数

在这种情况下,将使用rfind()函数,该函数从右侧开始找到定义的子字符串,然后可以通过切片功能删除该子字符串。可以通过以下示例更好地理解它:

示例

def remove_substring(string, substring):
    index = string.rfind(substring)  #rfind() is used to find the highest index of the substring in the string
    if index != -1 and index + len(substring) == len(string):    # If a substring is found, it is removed by slicing the string
        return string[:index]
    else:
        return string   #If no substring is found the original string is returned

# Example 
Whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
Final_String = remove_substring(Whole_string, last_substring)
print(Final_String)

输出

上面的代码的输出如下:

Hello Everyone, I am John

使用带捕获组的re模块

这是一种使用re模块来删除字符串末尾子串的不同方法。通过在正则表达式模块中使用捕获组,可以删除子串。以下是使用捕获组来删除子串的代码和示例:

示例:

import re #Do not forget to import the re module or error might occur while running the code

def remove_substring(string, substring):
    pattern = re.escape(substring) + r'(?=$)' # A function is created first and re.escape is used so that all the functions are created equally
    return re.sub(pattern, '', string) # With the help of re.sub() function, the substring will be replaced with empty place

# Example 
Whole_string = "Hello Everyone, I am John, the Sailor!"
last_substring = ", the Sailor!"
Final_String = remove_substring(Whole_string, last_substring)
print(Final_String)

输出

上述代码的输出结果如下:

Hello Everyone, I am John

结论

在全球范围内,用户都经常需要对字符串进行更改,但是如果不正确地进行处理,删除字符串的过程可能会耗费很多时间。因此,本文描述了上述许多不同的方法,可以用Python从字符串的末尾删除子字符串。可能存在其他方法来删除子字符串,但本文介绍的方法是最简短和最简单的方法,您可以根据应用领域进行选择。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程