Python Remove Character from String

Here, we will develop a Python program to remove character from string. If the string was “KnowProgram” then remove a specific character from the string and the new string will be like “Knowrogram”, “KnoProgram”, “nowProgram”, “Knowprogra”, etc. We will discuss how to remove characters from the given string using native methods, replace(), Regular Expression, translate(), join() method, and slice operator.

Remove Character from String Python

We will take a string while declaring the variables. Then, run a loop and append the characters and build a new string from the existing characters except when the index is i. (where i is the index of the character to be removed). Finally, a new string will be displayed on the screen.

# Python program to remove character from string

# take input
string = "Python"

# print original string
print ("Original string:", string)

# remove character from string
out_string = ""
for i in range(0, len(string)):
    if i != 4: 
        out_string = out_string + string[i]
   
# print string after removal
print ("New string:", out_string)

Output:-

Original string: Python
New string: Pythn

Remove All Occurrences of a Character in a String Python

We can use string.replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

Note:- That the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.

# Python program to remove character from string

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove characters using replace() function
out_string = string.replace('o', '')

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Knw Prgram

Replacing a Character in a String Python

In the above program, we had removed all occurrences of ‘o’ from the string but in this program, we are removing only 1 occurrence of ‘o’ from the string.

# Python program to remove character from string

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove character using replace() function
out_string = string.replace('o', '', 1)

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Knw Program

Remove Characters from String Python

We will first be importing Regular Expression (RegEx module). The re.sub() function will automatically remove the characters from the string.

# Python program to remove character from string

# importing RegEx module
import re

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove character using translate() method
out_string = re.sub('m', '', string)

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Know Progra

How to Delete a Character from a String in Python

The string.translate() function replaces each character in the given string using the given translation table. We have to specify the Unicode code point for the character and ‘None’ as a replacement to remove it from the result string. The ord() function returns the number representing the Unicode code of a specified character.

# Python program to remove character from string

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove character using translate() method
out_string = string.translate({ord(i): None for i in 'g'})

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Know Proram

Python Remove character from String by Index

In this program, every element of the string is converted to an equivalent element of a list, after which each of them is joined to form a string excluding the particular character to be removed.

# Python program to remove character from string

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove character at pos 6 using join() + list comprehension
out_string = ''.join([string[i] for i in range(len(string)) if i != 5])

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Know rogram

Python Program to Remove Character from String

Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end.

# Python program to remove character from string

# take input
string = "Know Program"

# print original string
print ("Original string:", string)

# remove character at pos 4 using slice + concatenation
out_string = string[:3] + string[4:]

# print string after removal
print ("New string:", out_string)

Output:-

Original string: Know Program
New string: Kno Program

Also See:- Python Program to Check Whether Character is Alphabet

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *