Remove Character from String in Python

The replace() function searches for a specified pattern in a string and replaces it with another specified string.

The search pattern is not case sensitive and the replacement can be either an empty string or any other valid Python expression.

Let’s say we have this sentence: “I like apples but I don’t like bananas”. If we want to replace the word “apples” with “oranges” then we can use this code:

This will replace the first word with the second one.

I like oranges but I don't like bananas.

Similarly, you can replace a single character with an empty space, in other words, remove it.

Another way to remove part of the string is to slice a string, which will also be presented in this lesson.

Remove all matching characters

The replace() function removes all characters from the string that are specified as an argument.

It will remove both letters “a” from the string.

I like pples but I don't like bnns.

Remove the first matching character

The third parameter inside the replace() function specifies the number of characters to replace. If you want to replace only the first matching character, you give 1 as the parameter.

This is the following string. Only the first “a” is removed.

I like pples but I don't like bananas.

Remove the specified number of matching characters

Similarly, you can change the third parameter to a different number, eg. 2. By doing this only the first two matching characters are replaced.

This will remove one “a” in “apples” and one in “bananas”.

I like pples but I don't like bnanas.

If you enter a number that is higher than the number of matching characters, it will remove all matching characters, therefore it works the same way as if there were no third parameter.

Remove the last matching character

If you want to remove the last matching character, first, you have to reverse the string.

To do it, you need to use this code.

Now, you can use the replace function for matching the first element, then reverse the string back.

You can also do the same thing for multiple matching characters. Just change the third parameter.

Remove the first character

To remove the first character from the string, you can assign the string to the same variable, excluding the first character.

This code assigns the my_string variable without the first character (from position 1 to the end; indexing starts from 0) to itself.

 like apples but I don't like bananas.

Remove the last character

In this example, there is a sentence with two exclamation marks at the end of the sentence. In this case, we can strip the last one, so the single one remains.

The part [:-1] inside a string means we are taking a string from the beginning to an end minus 1 character.

I like dogs and cats!

The only single exclamation mark remains as the last character is removed from the string.

Remove a character by index using a slicing

You can use slicing to remove a character inside a string. In this way, we are taking the first part of the string from the beginning to the specified index, and the second part from the specified index + 1 to the end of the string.

A single character between these two strings is dropped. If you add index + 2, it will remove two adjacent characters, etc.

Because indexing in Python starts from 0, index 3 will remove the fourth element.

I lke apples.