This post introduces how to remove / replace a character from a string using Python.
In Python, strings are immutable, so we need to create a new string. If you want to remove the ‘;’ wherever it appears. See below for an example.
Remove character”;”
>>> original_str = "good; ok" >>> new_str = original_str.replace(";","") >>> print(new_str) good ok >>>
See the examples below for different ways to check whether a string contains a specific character.
>>> s = "good; ok" >>> ";" in s True >>> ";" not in s False >>> s.find(";") == -1 False >>> s. find(";") != -1 True >>> chars = set(";o") >>> if any((c in chars) for c in s): >>> print("Found") Found >>> import re >>> pattern = re.compile(r";o") >>> if pattern.findall(s): >>> print("Found") Found >>>
References:
How to delete a character from a string using python?
Remove all special characters, punctuation and spaces from string