Python – Escape reserved characters in Strings List
Given List of Strings, escape reserved characters in each String.
Input : test_list = [“Gf-g”, “be)s(t”]
Output : [‘Gf\\-g’, ‘be\\)s\\(t’]
Explanation : All reserved character elements escaped, by adding double \\.Input : test_list = [“Gf-g”]
Output : [‘Gf\\-g’]
Explanation : All reserved character elements escaped, by adding double \\.
Method #1 : Using join() + list comprehension
In this, we construct the dictionary to map each of reserved character to its escaped version, and then perform the task of replacement in list comprehension and join the result to form Strings.
Python3
# Python3 code to demonstrate working of# Escape reserved characters in Strings List# Using list comprehension + join()# initializing listtest_list = ["Gf-g", "is*", "be)s(t"]# printing stringprint("The original list : " + str(test_list))# the reserved stringreserved_str = """? & | ! { } [ ] ( ) ^ ~ * : \ " ' + -"""# the mapped escaped valuesesc_dict = { chr : f"\\{chr}" for chr in reserved_str}# performing transformation using join and list comprehensionres = [ ''.join(esc_dict.get(chr, chr) for chr in sub) for sub in test_list]# printing resultsprint("The resultant escaped String : " + str(res)) |
The original list : ['Gf-g', 'is*', 'be)s(t'] The resultant escaped String : ['Gf\\-g', 'is\\*', 'be\\)s\\(t']
Method #2 : Using maketrans() + translate() + zip()
In this, the escaping is made by pairing using zip() and maketrans() rather than dictionary for mapping. The translation is done using the result of maketrans().
Python3
# Python3 code to demonstrate working of# Escape reserved characters in Strings List# Using maketrans() + translate() + zip()# initializing listtest_list = ["Gf-g", "is*", "be)s(t"]# printing stringprint("The original list : " + str(test_list))# reserved_charsreserved_chars = '''?&|!{}[]()^~*:\\"'+-'''# the escaping logicmapper = ['\\' + ele for ele in reserved_chars]result_mapping = str.maketrans(dict(zip(reserved_chars, mapper)))# reforming resultres = [sub.translate(result_mapping) for sub in test_list]# printing resultsprint("The resultant escaped String : " + str(res)) |
The original list : ['Gf-g', 'is*', 'be)s(t'] The resultant escaped String : ['Gf\\-g', 'is\\*', 'be\\)s\\(t']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


