How to remove a character from a string in Python

  • To remove any character the easiest way both in Python2.7 and python3+ is to use python built-in replace function. The function replaces characters with a substitute, which could be empty character too. Let’s say we want to remove underscore (‘_‘) character:
my_string_var = 'this_is@there'
my_string_var = my_string_var.replace('_', '')

The result would be ‘thisis@there

  • Done!