Remove a Character From String In Python

In this tutorial, you will learn to remove a character from String in Python in 5 different ways.

To remove a specific character from a string in Python, use one of the methods listed below.

  • Using the Naive method
  • Using the replace() function
  • Through the use of slice and concatenation
  • With the help of join() and list comprehension
  • Using the translate() method

It is important to note that in Python, the string is immutable. As a result, the original string remains unchanged, and these methods return a new string.

1. Using The Naive Method

Unless the index is n, we must run a loop and append the characters to the end of the existing string before creating a new string from the characters already in the string. (where n is the index of the character that is to be removed).

#Initializing the String
initString = "ABCDE Remove C"

#printing the original String
print(initString)

#Using the Naive Method Of looping
#To get the Character Removed
resultString = ""

for i in range(0, len(initString)):
    if i!=2:
        resultString += initString[i]

#Printing The Result String
print(resultString)

Output:

ABCDE Remove C
ABDE Remove C

2. Using The Replace Function To Remove A Character From String In Python

In Python, you can use the replace function from the string class. As a function, it takes two arguments: the first is the character you want to replace, and the second is the character you want to replace the first with.

Let us see in the below example code the usage of replace function to remove a character from a string in Python.

#Initializing the String
initString = "TEAST String"

#printing the original String
print(initString)

#Using the Replace Method
#To get the Character Removed
resultString = initString.replace('A', '')

#Printing The Result String
print(resultString)

Output:

TEAST String
TEST String

3. Using Slicing and Concatenation Method

Slicing is a Python feature that allows you to access specific parts of sequences such as strings, tuples, and lists. You can also use them to change or remove items from mutable sequences such as lists.

Let us see in the below code example how you can use the slicing and concatenation method to remove a character.

#Initializing the String
initString = "Correact String"

#printing the original String
print(initString)

#Using the Replace Method
#To get the Character Removed
resultString = initString[:5] + initString[6:]

#Printing The Result String
print(resultString)

Output:

Correact String
Correct String

As you can see in the code above, I divided the string into two parts. In the first part, we take the string up to index 4. (as “:5” means to include index up to 4).

And the second part of the string begins at index 6, implying that I ignored the character at index 5 and then concatenated the string to obtain the result.

4. Using join() method and list Comprehension Method

The string is converted into an equivalent element of a list, and then each of the equivalent elements is joined together to form a string that excludes the specific character to be removed, using this technique.

Let’s look at how to remove a character from a string using list comprehension and the join method in the code below.

#Initializing the String
initString = "Correact String"

#printing the original String
print(initString)

#Using the Replace Method
#To get the Character Removed
resultString = "".join([initString[i] for i in range(0, len(initString)) if i!=5])

#Printing The Result String
print(resultString)

Output:

Correact String
Correct String

5. Using translate() Method

In Python, you can use the string class’s translate method to remove or replace a character or part of a string from a given string. Return a copy of the string with each character mapped using the provided translation table.

Let’s look at how to use Python’s translate method to remove a character in the code below.

#Initializing the String
initString = "Correa123ct String"

#printing the original String
print(initString)

#Using the Replace Method
#To get the Character Removed
resultString = initString.translate({ord(i):None for i in 'a123'})

#Printing The Result String
print(resultString)

Output:

Correa123ct String
Correct String
Remove a Character From String In Python

Wrap Up

This tutorial covered five different methods for removing a character, and I hope you gained some new knowledge in the process.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Append Or Add String In Python
  2. How To Print A New Line In Python
  3. How To Downgrade Python

Leave a Comment