10 examples of 'how to remove special characters from a string in python' in Python

Every line of 'how to remove special characters from a string in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
34def replace_escape_char(a_string):
35
36 return _replace_by_regex(a_string, r'\[\s|\n]*')
42def remove(value, delete_chars):
43 for c in delete_chars:
44 value = value.replace(c, '')
45 return value
202def remove_newlines(string):
203 string = string.replace("\n", "")
204 return string.replace("\r", "")
78def clean_string(string):
79 string = re.sub('@', '@\u200b', string)
80 string = re.sub('#', '#\u200b', string)
81 string = re.sub('`', '', string)
82 return string
86def stripIllegalChars(str):
87
88 # If the char separates a word, e.g. Face/Off, we need to preserve that separation with a -
89 str = re.sub(r'(?<=\S)[' + patterns.illegalChars + r'](?=\S)', '-', str)
90
91 # If it terminates another word, e.g. Mission: Impossible, we replace it, and any surrounding spaces with a single space
92 str = re.sub(r'\s?[' + patterns.illegalChars + r']\s?', ' ', str)
93
94 return str
76def _clean_win_chars(string):
77 """Windows cannot encode some characters in filename."""
78 import urllib
79 if hasattr(urllib, 'quote'):
80 quote = urllib.quote
81 else:
82 # In Python 3, quote is elsewhere
83 import urllib.parse
84 quote = urllib.parse.quote
85 for char in ('<', '>', '!', ':', '\\'):
86 string = string.replace(char, quote(char))
87 return string
97def escape_string(string):
98 result = string.replace("'", "''")
99 return result
158def remove_newlines(value):
159 if not value:
160 return ''
161 elif isinstance(value, six.string_types):
162 return value.replace('\r', '').replace('\n', '')
163 else:
164 return value
448def remove_control_characters(s):
449 if s is None:
450 return ''
451
452 s = unicode(s, "utf-8")
453 return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
41def escape(string):
42 """
43 simple escaping of &, <, and >
44 """
45 return string.replace("&", "&").replace("<", "<").replace(">",
46 ">")

Related snippets