You are currently viewing How to Remove a Character from String in Python

How to Remove a Character from String in Python

If you want to remove a character from string in Python, you can use the string.replace() method. This method takes two arguments, the first is the character you want to remove, and the second is the replacement character. For example, if you wanted to remove the letter o from the method, you would use the following code

Example 1: Remove Character from a Word

str = "method"
final_str = str.replace("o", "")
print(str)
print(final_str)

Output:

method
methd

Example 2: Remove Character from a Sentence

str = "example of character removal."
final_str = str.replace("a", "")
print(str)
print(final_str)

Output:

example of character removal.
exmple of chrcter removl.

Reference:

https://docs.python.org/3/

Leave a Reply