How to remove Substring From String in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to remove Substring From String in Python. Here i am giving you some methods which will be useful for to remove the substring from your string. It will also be useful for to replace the substring from your string in python. So lets learn about of this methods. I hope this will help you.

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(). You can use string.replace to replace your substring from your string and you can put there the another substring. So lets learn how this method work. Here you have to just use the string.replace function for to remove the substring from your string. So lets learn this method by given below example. I hop you guys like it. mystr = "Lets learn python" A = mystr.replace("n", "0", 2) print(A) Output : Lets lear0 pytho0

  2. How to remove Substring From String in Python

    to remove Substring From String in Python just Use str.removesuffix(). By using str.removesuffix() you can remove the substring from your string in python. Here we will use str.removesuffix() function to remove the substring which we don't want in our string. So first all of all we will take str.removesuffix() function with the substring we want to remove from our string and then print it. It will simply remove it from your string. Let learn this by given below example. I hope you like it. mystr = "Lets learn python" print(mystr.removesuffix('python')) print(mystr.removesuffix('World')) Output : Lets learn Lets learn python

Method 1: Use string.replace()

You can use string.replace to replace your substring from your string and you can put there the another substring. So lets learn how this method work. Here you have to just use the string.replace function for to remove the substring from your string. So lets learn this method by given below example. I hop you guys like it.

mystr = "Lets learn python"
A = mystr.replace("n", "0", 2)
print(A)

Output :

Lets lear0 pytho0

Method 2: Use str.removesuffix()

By using str.removesuffix() you can remove the substring from your string in python. Here we will use str.removesuffix() function to remove the substring which we don’t want in our string. So first all of all we will take str.removesuffix() function with the substring we want to remove from our string and then print it. It will simply remove it from your string. Let learn this by given below example. I hope you like it.

mystr = "Lets learn python"
print(mystr.removesuffix('python'))
print(mystr.removesuffix('World'))

Output :

Lets learn 
Lets learn python

Method 3: Use str.replace()

You can use str.replace() to replace the substring from your string. Here i am making my string which contains substring and then using str.rplace() function and then print it. It will remove the substring which i want to remove. lets learn this by given below example. I hope this will help you. Lets learn it.

mystr = {'maths.48', 'science.49', 'english.48', 'ss.49'}
newset = {x.replace('.48', '').replace('.49', '') for x in mystr}
print(newset)

Output :

{'ss', 'maths', 'english', 'science'}

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment