
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 do I do a case insensitive string comparison in Python?
- How do I execute a string containing Python code in Python?
- How do I wrap a string in a file in Python?
- How to process escape sequences in a string in Python?
- How important is backslash in breaking a string in JavaScript?
- 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
- How do I serialize a Python dictionary into a string and vice versa?
- How do I remove a substring from the end of a string in Python?
- How do I create a Python namespace?
- How do I check if a string has alphabets or numbers in Python?
- How do I copy a file in python?
Advertisements