chevron_left
0 points
‚óŹ3

Here are 3 simple methods to remove a substring from a string in python.

while working with Python Strings, we can have problem in which we need to remove a substring from String. This is quite easy and many times solved before. But sometimes, we deal with list of strings that need to be removed and String adjusted accordingly. Lets discuss certain ways in which this task is achieved.

 

Method #1 : Using loop + replace()
The combination of above functions can be used to solve this problem. In this, we perform the replace of multiple Strings as they occur using replace().

 
 

Python3 code to demonstrate working of 

# Remove substring list from String

# Using loop + replace()

  

# initializing string

test_str = "gfg is best for all geeks"

  

# printing original string

print("The original string is : " + test_str)

  

# initializing sub list 

sub_list = ["best", "all"]

  

# Remove substring list from String

# Using loop + replace()

for sub in sub_list:

    test_str = test_str.replace(' ' + sub + ' ', ' ')

  

# printing result 

print("The string after substring removal : " + test_str) 

Output : 

The original string is : gfg is best for all ge

 

The string after substring removal : gfg is for geeks

 

Method #2 : Using replace() + join() + split()
The combination of above functions can be used to solve this problem. In this, we perform the task of handling single space using join() + split().

 
 

 

# Python3 code to demonstrate working of 

# Remove substring list from String

# Using replace() + join() + split()

  

# initializing string

test_str = "gfg is best for all geeks"

  

# printing original string

print("The original string is : " + test_str)

  

# initializing sub list 

sub_list = ["best", "all"]

  

# Remove substring list from String

# Using replace() + join() + split()

for sub in sub_list:

    test_str = test_str.replace(sub, ' ')

res = " ".join(test_str.split())

  

# printing result 

print("The string after substring removal : " + res) 

Output : 

The original string is : gfg is best for all geeks
The string after substring removal : gfg is for geeks

Method #3: Using Naive Method

# Python3 code to demonstrate

# to remove a substring from end of the string

  

# Initialising string

ini_string = 'xbzefdgstb'

  

# initializing string

sstring = 'stb'

  

# printing initial string and substring

print ("initial_strings : ", ini_string, "\nsubstring : ", sstring)

  

# removing substring from end

# of string using Naive Method

if ini_string.endswith(sstring):

    res = ini_string[:-(len(sstring))]

  

# printing result

print ("resultant string", res)

Output:

initial_strings :  xbzefdgstb 
substring :  stb
resultant string xbzefdg

More Posts

HOW TO REMOVE A SUBSTRING FROM STRING IN PYTHON - Oct 23, 2020
How to Remove a Substring from a String in Python hubconcepts - Oct 21, 2020
How to remove the last character from string in php? Farkhanda Athar - May 14, 2020
How to split a string in java Hasnain_khan - Oct 10, 2020
Python get substring between two characters amna - Apr 13
How to remove the last character from the string in C++? Lalit Kumar - Jun 19, 2020
How to remove the first character from the string in C++? Lalit Kumar - Jun 19, 2020
find a word in a string python SharadMagar450 - Aug 7
How to get a substring in C (CPP) raven - Jul 29
How to remove duplicate characters from string in python amna - May 19