69.6k views
5 votes
How to Remove a Specific Character from a String in Python?

1 Answer

1 vote

Final answer:

To remove a character from a string in Python, use the replace method with the character to remove and an empty string as arguments. The replace method creates a new string with the desired character removed.

Step-by-step explanation:

To remove a specific character from a string in Python, you can use the replace method. The replace method takes two arguments: the character to be replaced and the character to replace it with, which in the case of removal is an empty string. Here's a simple example:

my_string = "Hello, World!"
# Remove the character 'l'
modified_string = my_string.replace('l', '')
print(modified_string) # Output: Heo, Word!

Remember that strings in Python are immutable, meaning that the replace method does not change the original string; it returns a new string with the replacements.

User Melug
by
7.5k points