Backslash Escaped Strings को Unescape करना
\\ को \ से Replace करने या Backslash Escaped Strings को Unescape करने के दो तरीके हैं।
तरीका 1: literal_eval का उपयोग (सुरक्षित)
literal_eval का उपयोग करके String को Evaluate करें। ध्यान दें कि इस Method में आपको String को Quotes की Another Layer से Surround करना होगा।
>>> import ast >>> a = '"Hello,\\nworld"' >>> print(ast.literal_eval(a)) Hello, world
तरीका 2: decode('string_escape') का उपयोग (Python 2 केवल)
>>> print("Hello,\\nworld".decode('string_escape'))
Hello,
world
Python 3 में Equivalent:
>>> print("Hello,\\nworld".encode().decode('unicode_escape'))
Hello,
world
अन्य Escape Sequences:
>>> import ast
>&>> ast.literal_eval('"Tab:\\tQuote:\\"Path: C:\\\\Users"')
'Tab:\tQuote:"Path: C:\\Users'
Raw Strings का उपयोग (Prevention):
# Raw String - Backslashes को Literal मानता है path = r'C:\Users\Name\Documents' print(path) # C:\Users\Name\Documents # Normal String - Escape Sequences Process होते हैं path = 'C:\\Users\\Name\\Documents'