5 Ways to Remove Letter From a String in Python

Hello, friends, in this tutorial we will learn 5 ways to remove the letter from a string in python. You can remove that specific letter or you can replace it with another letter.

To remove the letter from a string, we will use a replace(), partition(), sub(), strip(), and array index methods.

Let’s see the examples of python code to remove the letter from a string.

Python replace() Method

In this replace() method, we will replace the specific character from a string and return the string after removing the letter.

It will take three parameters, the first parameter refer to the old value, the second parameter refers to the new value and the third parameter refer to the count of occurrences of the old value that want to replace.

Output
Python!!!

Python partition() Method

The partition() method search for a specified string and splits the string into a tuple and returns the three elements.

The first element is the part before the specified string, the second is the specified string, and the third is the part after the specified string.

Output
Python!!!

Python sub() Method

The sub() method is built in re module functions and it takes 5 parameters. It works with a regular expression and searches for the match pattern and replaces it.

Output
Python!!!

Python strip() Method

The strip() method is used to remove all the spaces from the beginning and trailing. You can also specify the characters that you want to remove from a string.

Output
Python!!!

Python 5th Method – Using String Slicing

In this method, we will slice the string specifing start and end index of the string. Then it will return the specified index characters.

Output
Hello

That’s it!!!

So these are the 5 ways to remove the letter from a string in python. There are more ways to remove characters from strings like using the join(), translate() methods, etc.

Being Tricky 😉

Leave a Reply

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