Table of Contents
Use replace()
Method
To eliminate a substring from string in Python:
- Define and initiate
my_string
. - Declare
substring
to remove it frommy_string
. - Use
replace()
method to removesubstring
from string. e.g.my_string = my_string.replace(substring, "")
1 2 3 4 5 6 7 8 9 10 11 |
my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My String: {my_string}') my_string = my_string.replace(substring, "") print(f'Substring: {substring}\n' f'My New String: {my_string}') |
The code above will print the following output on the console:
1 2 3 4 5 |
My String: The code $#jc@ snippet removes a substring $#jc@ from a string in python. Substring: $#jc@ My New String: The code snippet removes a substring from a string in python. |
We tried removing the substring
from my_string
.
The replace() is a built-in method in Python that holds two or more parameters and a __count
of the occurrences of a substring.
If specified, at most __count
substitutions are performed from left to right. Finally, the method replaces all the occurrences of the substring old with new and returns a copy.
We used replace(substring, "")
on my_string
as:
__count
by default is-1
, but you can set it usingreplace(my_string, "", 2)
.- The
__old
string is thesubstring
. - The
__new
string""
is nothing but an empty string.
F-strings, also known as "formatted string literals", is a new way in Python to format strings. We used it to provide a better display of information as f'Input: {my_string}'
.
Use the split()
Method with for
loop
To get rid of a substring from a string in Python:
- Declare an empty string
my_new_string = ""
- Use
split()
to splitmy_string
bysubstring
into arraystr_arr
. - Use the
for
loop to iterate over eachelement
ofstr_arr
- Concatenate every
element
ofmy_string
withmy_new_string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My String: {my_string}') my_new_string = "" str_arr = my_string.split( sep=substring, maxsplit=2 ) for element in str_arr: my_new_string += element print(f'Substring: {substring}\n' f'My New String: {my_new_string}') |
The code above will print the following output on the console:
1 2 3 4 5 |
My String: The code $#jc@ snippet removes a substring $#jc@ from a string in python. Substring: $#jc@ My New String: The code snippet removes a substring from a string in python. |
Python String Concatenation requires declared and initialized strings. my_new_string
is ""
that is just an empty string.
We used split()
to discard the substring
from my_string
.
The split()
method performed as:
- Took
sep
as a delimiter. - Took
maxsplit
(default is-1
).maxsplit
defines number of maxium split. - Discarded
maxsplit
occurrences ofsep
from the resultant string. - Split the string at
maxsplit
points. - Returned a copy of a list of strings(s).
We used a for
loop to iterate over all the elements of the my_string
.
We used +=
to join the String.
In Python, we can perform String Concatenation using +=, holding the resultant string on the left side as my_new_string = my_string
.
If you want to remove only first occurrence of substring, then you can use maxsplit
as 1 while using split()
and it won’t remove other occurrences of the substring.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My String: {my_string}') my_new_string = "" str_arr = my_string.split( sep=substring, maxsplit=1 ) for element in str_arr: my_new_string += element print(f'Substring: {substring}\n' f'My New String: {my_new_string}') |
The code above will print the following output on the console:
1 2 3 4 5 |
My String: The code $#jc@ snippet removes a substring $#jc@ from a string in python. Substring: $#jc@ My New String: The code snippet removes a substring $#jc@ from a string in python. |
If you want to remove all occurrences of the substring, don’t pass
maxsplit
parameter as it is defaulted to -1 and that means there won’t be any limit to number of splits.
Further reading:
Use split()
with join()
Method
To eliminate a substring from a string in Python:
- Use
split()
to remove thesubstring
frommy_string
- Use the
join()
method to join every element of the listmy_string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My String: {my_string}') my_string = my_string.split( sep=substring, maxsplit=2 ) my_string = "".join(my_string) print(f'Substring: {substring}\n' f'My New String: {my_string}') |
The code above will print the following output on the console:
1 2 3 4 5 |
My String: The code $#jc@ snippet removes a substring $#jc@ from a string in python. Substring: $#jc@ My New String: The code snippet removes a substring from a string in python. |
We covered split()
while explaining the code using the split()
method.
We used the join()
method in the above code snippet. It took an iterable object of strings to concatenate them where the delimited string is the one that provides that method. For example, we concatenated the elements of my_string
using the delimiter ""
.
Use re.sub()
Method
To eliminate a substring from a string in Python:
- Import
re
- Use
re.sub()
to remove thesubstring
frommy_string
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import re my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." print(f'My String: {my_string}') my_string = re.sub( pattern='\W\Wjc\W ', repl='', string=my_string ) print(f'My New String: {my_string}') |
The code above will print the following output on the console:
1 2 3 4 |
My String: The code $#jc@ snippet removes a substring $#jc@ from a string in python. My New String: The code snippet removes a substring from a string in python. |
We imported re library that provides regular expression-matching operations
The re.sub()
is a method of the re
library that substitutes occurrences of a pattern found in a string. It held:
- The original
string
. - A replaceable string
pattern
. - A
count
(default is0
). - A
repl
string to replace thecount
timespattern
.
You may wonder why we write the pattern
as \W\WJC\W
because the re
library cannot nest special characters directly; we replaced each with the escape sequence \W
.
We used re.sub()
to replace the count
times pattern
of my_String
with ""
. The count
by default is 0
and it will replace all the occurrences but you can set it using count=1,2,3,...
.
That’s all about how to remove substring from String in Python.