close

How to remove substring from string in python

Here in this tutorial we are going to learn about How to remove substring from string in python. Sometimes we have face some problem white working with string then we have to remove some substring from the string. There this tutorial will help you. Here we guys going to learn about of this. So without discussing anything lets go to the different methods.

How to remove substring from string in python

  1. remove substring from string in python

    to remove substring from string in python just Use string.replace().string.replace() is used for replace the substring by other string. There are we use old string , new string and the count number which indicates how many time we want to replace this substring. By below example you will better understand. Lets learn this. mystr = "Good afternoon!" result = mystr.replace("o", "0", 4) print(result) Output : G00d aftern00n!

  2. How to remove substring from string in python

    to remove substring from string in python just Use str.replace().Just use str.removesuffix to remove the substring from string. By using this you can easily remove the sub string from your string. So without wasting time lets learn about of this. mystr = 'sunday' print(mystr.removesuffix('day')) print(mystr.removesuffix('World')) Output : sun sunday

Method 1: Use string.replace()

string.replace() is used for replace the substring by other string. There are we use old string , new string and the count number which indicates how many time we want to replace this substring. By below example you will better understand. Lets learn this.

mystr = "Good afternoon!"
result = mystr.replace("o", "0", 4)
print(result)

Output :

G00d aftern00n!

Method 2: Use str.replace()

By using str.replace() you can replace substring by empty string or another string. if you want to remove sub string than replace it with empty string. So without wasting time lets learn about of it by given below example:

mylist = {'sunday','monday','tuesday','wednesday','thursday','friday','saturday'}
new = {x.replace('day', '') for x in mylist}
print(new)

Output :

{'sun', 'tues', 'mon', 'wednes', 'thurs', 'satur', 'fri'}

Method 3: Use str.removesuffix

Just use str.removesuffix to remove the substring from string. By using this you can easily remove the sub string from your string. So without wasting time lets learn about of this.

mystr = 'sunday'
print(mystr.removesuffix('day'))
print(mystr.removesuffix('World'))

Output :

sun
sunday

Method 4: Use loop + replace()

You can use loop and replace() function together to remove the substring from your string. From your string you can remove the substring and put an empty string there by using loop and replace function. So that there is some substring which is creating a problem for you there you can use such a method. I hope you guys like this method. Its very easy to use and very simple. Lets learn this by given below example:

mystr = "Pizza is my favourite food."
print("Original string : " + mystr) 
mylist = ["my", "food"]
for sub in mylist:
    mystr = mystr.replace(' ' + sub + ' ', ' ')
print("After substring removal : " + mystr) 

Output :

Original string : Pizza is my favourite food.
After substring removal : Pizza is favourite food.

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment