Escape Characters

Python · Strings

Theory

Some characters cannot be typed directly inside a string literal, so Python uses escape sequences that start with a backslash. \n is a newline, \t is a tab, \ is one literal backslash, and \' or \" puts a quote inside a string that uses the same quote. Each escape sequence becomes a single character in memory: len('\n') is 1. print('Line1\nLine2') shows two lines.

Prefixing a literal with r creates a raw string, in which backslashes are kept as ordinary characters: len(r'\n') is 2. Raw strings are the standard way to write Windows paths like r'C:\new' (without the r, \n would become a newline) and regular expressions, which use many backslashes. Another way to avoid escaping quotes is simply to choose the other quote type: "It's" needs no escape. When you print the repr() of a string, Python shows the escapes back to you, which helps find hidden newlines or tabs in data.

Why we use it

Text often needs line breaks, tabs, quotes and backslashes. Escape sequences let you put them in a literal, and knowing them helps you debug data with invisible characters like stray \r\n line endings.

When to use it

Use \n and \t when building multi-line or tab-separated output. Use raw strings for Windows paths and regex patterns. Use repr() to reveal hidden escape characters when cleaning data.

How it works

  1. Python reads a string literal
  2. Is it a raw string (r prefix)? Yes: Keep every backslash as a normal character. No: Look for backslash sequences.
  3. Found a known escape like \n or \t? Yes: Replace the pair with one special character. No: Keep the characters as typed.
  4. Store the final characters as the string object
  5. print() shows the real newline or tab

Syntax

\n  \t  \'  \"  \

Example

print('Line1\nLine2\tTabbed')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic