Python String Escape and Unescape Tool

Escape plain text for use inside Python string literals, or decode supported Python-style escape sequences back into readable text. Convert quotes, backslashes, line breaks and Unicode escapes locally in your browser without executing Python code.

Processed locally in your browser

Input is treated as text content, not executable Python source.

The selected delimiter controls whether single quotes or double quotes are escaped.

When enabled, non-ASCII characters use Python-compatible \xNN, \uNNNN or \UNNNNNNNN escapes.

Advanced options

Optional normalization may change how some Unicode characters are represented.

Escape or unescape a Python string

This Python string escape tool prepares plain text for a Python string literal and decodes supported Python escape sequences back into readable text. It escapes quotes according to the selected quote style, escapes backslashes and common control characters, optionally converts non-ASCII characters, and runs locally in the browser. It does not add a variable assignment, parse complete Python source, call eval, import modules or evaluate expressions.

Python string escape examples

Plain-text input:

User "admin"
Path: C:\Tools\Unicode
Message: café

Escaped for a double-quoted Python string:

User \"admin\"\nPath: C:\\Tools\\Unicode\nMessage: café

The tool returns escaped string content only. Placed in Python source, that content can be used like this:

message = "User \"admin\"\nPath: C:\\Tools\\Unicode\nMessage: café"

Single-quoted example:

message = 'It\'s stored in C:\\Tools\\Unicode'

Single quotes vs double quotes in Python

Python supports both single-quoted and double-quoted strings. The selected delimiter determines which quote character must be escaped, while backslashes and control characters still need consistent escaping. This converter does not unnecessarily escape the opposite quote character.

Input character or sequenceSingle-quoted modeDouble-quoted mode
Backslash\\\\
Single quote\'Preserved
Double quotePreserved\"
Newline\n\n
Tab\t\t
Non-ASCII characterPreserved by default, escaped when selected.Preserved by default, escaped when selected.

Python escape sequences supported by this tool

Unescape mode supports one layer of \\, \', \", \n, \r, \t, \b, \f, \v, \xNN, \uNNNN and \UNNNNNNNN. Malformed sequences such as \xG1, short Unicode escapes or out-of-range values are preserved and reported instead of being silently corrupted. The tool does not implement \N{name}, octal escapes, raw-string parsing or full Python source parsing.

How to escape quotes and backslashes in Python

Escape double quotes when the content will be placed inside a double-quoted Python string, and escape single quotes when it will be placed inside a single-quoted string. Backslashes are escaped as \\, so a Windows path such as C:\Tools\Unicode becomes C:\\Tools\\Unicode in a normal Python string literal.

Python Unicode escape sequences

Unicode characters are preserved by default. When “Escape non-ASCII characters” is enabled, this tool uses \xNN for U+0080 through U+00FF, \uNNNN through U+FFFF, and \UNNNNNNNN for supplementary-plane code points. Unescape mode validates code-point ranges and does not generate invalid surrogate output.

Raw strings and Windows paths

Python raw strings reduce the need to escape backslashes, which can make Windows paths and regular expressions easier to read. For example, r"C:\Tools\Unicode" preserves the backslashes. Raw strings still have syntax limitations and cannot end with an odd number of backslashes. A normal Python string path can be written as path = "C:\\Tools\\Unicode".

How to use the Python string converter

  1. Paste plain text or escaped Python-style text into the input field.
  2. Select Escape or Unescape.
  3. Choose single-quoted or double-quoted output.
  4. Optionally enable non-ASCII escaping or Unicode normalization.
  5. Process the text, then copy or download the result.

repr() vs escaped string content

repr() returns a developer-oriented representation and usually includes surrounding quote characters. This page returns escaped string content by default, so you can paste the result between your chosen quote delimiters. Results may differ from Python’s version-specific repr() details because quote style and non-ASCII handling are explicit controls here.

Privacy and local processing

This tool processes text with JavaScript directly in your browser. Your input is not submitted to UnicodeNow servers. Input is treated only as text, and the converter does not execute Python code, import modules or evaluate expressions.

Frequently asked questions

How do I escape a string in Python?

Choose Escape, select the quote style, paste text content and process it. The output is escaped string content that can be placed between your chosen Python quote delimiters.

How do I escape quotes in a Python string?

Use double-quoted mode to escape double quotes as \" or single-quoted mode to escape single quotes as \'. The opposite quote character is left alone unless another option requires escaping.

How do I unescape Python escape sequences?

Choose Unescape and paste supported Python-style escape notation such as \n, \t, \xNN, \uNNNN or \UNNNNNNNN. The parser decodes one syntax layer without executing Python.

What is the difference between repr() and string escaping?

repr() returns a developer-oriented representation and usually includes quote characters. This tool returns escaped string content by default, with behavior controlled by the selected quote style and non-ASCII option.

How do Python Unicode escape sequences work?

\xNN represents a two-digit byte-sized value, \uNNNN represents a BMP code point and \UNNNNNNNN represents a supplementary code point. Invalid Unicode scalar values are preserved and reported.

Should I use a raw string for Windows paths?

Raw strings can make paths easier to read, such as r"C:\Tools\Unicode", but they still have syntax limits and cannot end with an odd number of backslashes.

Does this tool execute Python code?

No. Input is treated as text content only. The converter does not execute Python code, call eval, import modules or evaluate expressions.

Is my input sent to a server?

No. This browser-local converter processes text with JavaScript and does not submit your input to UnicodeNow servers.

Related tools

Working with another format? Use the JSON Escape and Unescape tool for JSON string content, the JavaScript Escape and Unescape tool for JavaScript literals, or the Unicode Escape Converter for sequences such as \u00E9.

PHP Escape and Unescape

Escape and unescape PHP-style single and double quoted strings.

DeveloperProcessed locally

Unicode Escape Converter

Convert text to and from Unicode escape sequences and numeric entities.

DeveloperProcessed locally

XML Escape and Unescape

Escape and decode XML entities and numeric character references.

DeveloperProcessed locally

Related guides

Unicode vs UTF-8

Unicode defines characters and code points. UTF-8 encodes those code points as bytes for files, databases, web pages, APIs and network messages.

What Is a Unicode Code Point?

Learn what Unicode code points are, how U+ notation works, and how code points differ from glyphs, bytes, code units and visible characters.