How to Remove a Substring from a String in Python
This tutorial explains how to delete a substring in a string in Python. It will show you that strings cannot just be deleted, but only replaced. This tutorial also lists some sample codes to clarify the concepts, as the method has changed compared to previous Python versions.
str.replace()
Replace substring from String using method in Python 3.x
Strings have many built-in methods. In fact, strings are immutable in Python. You can str.replace()
create a new string using the replace method. str.replace(oldvalue, newvalue, count)
Returns a copy of the string with the replaced oldvalue
by newvalue
. count
tells how many times the replacement should be performed.
list_str = {"Abc.ex", "Bcd.ex", "cde.ex", "def.jpg", "efg.jpg"}
new_set = {x.replace(".ex", "").replace(".jpg", "") for x in list_str}
print(new_set)
Output:
{'Bcd', 'Abc', 'cde', 'def', 'efg'}
string.replace()
Use the method to replace a substring in a string in Python 2.x
If you are using Python 2.x, you can use string.replace()
the replace method to replace a substring. This method takes old value
, , new value
and count
as arguments. new value
is what replace old value
requires and count
is a number specifying the number of occurrences of the old value you want to replace. The default value is all occurrences.
The sample code for this method is as follows.
text = "Hello World!"
x = text.replace("l", "k", 1)
print(x)
Output:
Heklo World!
str.removesuffix()
Remove the suffix from the string using
If you are using Python 3.9, you can str.removesuffix('suffix')
remove the suffix using .
If the string ends with the suffix string, and the suffix is non-empty, the string is returned with the suffix removed. Otherwise, the original string is returned.
A basic example of is given below str.removesuffix()
.
text = "Quickly"
print(text.removesuffix("ly"))
print(text.removesuffix("World"))
Output:
Quick
Quickly
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in
How to update Python on Mac
Publish Date:2025/05/07 Views:96 Category:Python
-
In this tutorial, we will discuss different ways to update Python on your Mac. We will also discuss how to install the latest version of Python 3 or Python 2 on your Mac. While the easiest way to update or install the latest version of Pyth
How to print to stderr in Python
Publish Date:2025/05/06 Views:66 Category:Python
-
This tutorial explains how to write to standard error output in Python stderr . It also lists some sample code to explain different ways to write stderr , as the syntax has changed over time. In Python 3.x this prints stderr For Python 3.x,
How to wait for user input in Python
Publish Date:2025/05/06 Views:112 Category:Python
-
This tutorial shows you how to wait for a keypress before doing something else in Python. input() Waiting for input in Python input() Is a Python function that allows you to process user input in your code. It temporarily stops all processe
How to read input from stdin in Python
Publish Date:2025/05/05 Views:125 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
How to read specific lines from a file in Python
Publish Date:2025/05/05 Views:182 Category:Python
-
A common way to read files in Python is to read the file completely and then process specific lines. Reading files in Python is fast, for example, it takes about 0.67 seconds to write a 100MiB file. However, if the file size exceeds 100MB,
How to Randomly Select Elements from a List in Python
Publish Date:2025/05/05 Views:70 Category:Python
-
This tutorial showed you how to randomly select an element from a list in Python. There are multiple simple ways to achieve this goal, all of which involve importing Python modules. This tutorial will cover solutions that require the random
How to Replace an Element in a Python List
Publish Date:2025/05/05 Views:110 Category:Python
-
We can replace elements in Python lists in many ways. We can use Python list element indexing, for loops, map functions, and list comprehensions. This article will discuss the above methods to find and replace elements in Python lists. Sear
How to add an element to the front of a list in Python
Publish Date:2025/05/05 Views:192 Category:Python
-
This tutorial explains how to insert an element to the front of a list in Python. This tutorial also lists some sample codes to help you understand. insert() Use the method to insert an element to the front of a list in Python insert() One