W3docs

How to delete a character from a string using Python

You can use string slicing to remove a specific character from a string in Python.

You can use string slicing to remove a specific character from a string in Python. Here is an example:

Remove a specific character from a string in Python using index ranges

python— editable, runs on the server

In this example, the slice string[:5] includes all characters before the character at index 5, and the slice string[6:] includes all characters after the character at index 5. By concatenating these slices with the + operator, the character at index 5 is removed.

Another way of removing a character from a string is to use the replace() method. Here is an example:

Remove a specific character from a string in Python using the replace method

python— editable, runs on the server

In this example, the replace() method is used to replace all instances of the "," character with an empty string, effectively removing the character from the string.