Online Tutorials & Training Materials |
Register Login

Remove Character from String in Python

|| 0

Remove Character from String in Python

Sometimes either user or the programmer wants to remove a character from a string. But sometimes this requirement is more than a character, but a malicious character occurrence. This can be achieved with multiple string operations or the using functions.

replace() Function

This function has two arguments and used to replace the character. the function is used to replace a character with the new one provided in the second argument if there is provided none then given character will be removed from the string.

As we know that strings in python will remain unchanged it means given character will change but the rest string will remain the same after changes.



string = 'This is a string of characters'
str = string.replace(' ', '')
print("this output of string after changes:-"), str



In this example we have a string and we are removing the all spaces in this string and the out will be of this is

See the changes after removing (replacing) spaces. Observe with another example.

Remove the Vowels from given string.

string = 'This is a again a string of characters'
import re
string = re.sub('[aeiou]', '', string)
print("this is out of function:-"),string


this is out of function:- Ths s  gn  strng f chrctrs

translate() function

This is a string translate method, used to replace the character in the string using a translate table given prior. Here translate means to change the character. We have to specify for the character and none as a replacement to remove it from the result string. To replace / change the character we have to know about the Unicode for that we can use like ord() function.

Example 1:

# using translate()
charsTOremove = ['A', ':', '!',';', "*"] 
# initializing test string
string = "HeAek * s:fo ! r;he * e*k:s !"
# printing original string
print ("main String : " + string)
# using translate() to
string = string.translate(None, ''.join(charsTOremove))
# printing resultant string
print ("final string : " + str(string))


main String : HeAek * s:fo ! r;he * e*k:s !
final string : Heek  sfo  rhe  eks

Example 2:

M = 'Aabc43265258dbaA'
print(M.translate({ord('a'): None}))



Removing Spaces from a String

Remove all space from a given string. Using both function translate and replace function.


# Program to Remove all spaces from a given string 
def Sspaceremove(string): 
	list = [] 
	for i in xrange(len(string)): 
		if string[i] != ' ': 
	return toString(list) 
def toString(List): 
	return ''.join(List) 
# Driver program 
string = "everything   gone out side the world "
print Sspaceremove(string)



Example 2

string = 'string for 1 2 3 4 '
print(string.replace(' ', ''))  



Python Remove newline from String

while printing the string sometimes it goes to 2nd line according to the line break. To remove this line break we us replace and translate function.


string  = 'inndina is great'
print(string.replace('n', ''))
print(string.translate({ord('n'): None}))


india is great      
india is great

Remove substring from string

To remove a substring from the string can be done using the replace() function.

We know there are two arguments have to passed with function, first one is to replace and the second to replace with, but there is nothing given in the list then it will removed from the main string.


string = 'this is main string'
print(string.replace('this', ''))


is main string

Remove specified number of times

To remove a string or an occurrence of a character from main string we use replace () function, in which can also pass 3 arguments. One is same what to search, in second argument what to remove and in third how many occurrence of that character to remove.


stng = 'this is a main a a string'
print(stng.replace('a', '', 2))


this is min a a string

See character a is removed from main string up to two occurrence of it rest are leave as it is.

Remove specific characters from a string in Python

To remove specific character from a string replace () is used.


stng = 'Hello people'
print(stng.replace('e', ''))


Hllo popl

Delete Character from String

strip () Function

strip() function used to deliver a copy of a string after removing both leading and trailing character from given string passed as a argument.


string = ' philips love philips   '
print(string.strip(' philipse'))


philips love philips


See here given string is Philips and character occurred is removed from string "philips love Philips", and returned ov only because Philips is given and l, e occurred in the string that's why they removed.

Related Articles