
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I un-escape a backslash-escaped string in Python?
There are two ways to go about unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:
>>> import ast >>> a = '"Hello,\nworld"' >>> print ast.literal_eval(a) Hello, world
Another way is to use the decode('string_escape') method from the string class. For example,
>>> print "Hello,\nworld".decode('string_escape') Hello, world
- Related Articles
- How do you make a string in PHP with a backslash in it?
- How can I remove the ANSI escape sequences from a string in python?
- How to Split a String with Escaped Delimiters?
- How to process escape sequences in a string in Python?
- How do I do a case insensitive string comparison in Python?
- How to write backslash in Golang string?
- How do I execute a string containing Python code in Python?
- How do I modify a string in place in Python?
- How do I wrap a string in a file in Python?
- How important is backslash in breaking a string in JavaScript?
- How do I convert a string to a number in Python?
- How do I convert a number to a string in Python?
- How do I Input a String From the User in Python?
- How do I format a string using a dictionary in Python 3?
- MySQL query to replace backslash from a varchar column with preceding backslash string values

Advertisements