🐍
Python Extra
  • Python: An Introduction
  • Python Getting Started
  • Python Syntax and Execution
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
    • Python - Slicing Strings
    • Python - Modify Strings
    • Python - String Concatenation
    • Python - Format Strings
    • Python - Escape Characters
    • Python - String Methods
  • Python Booleans
  • Python Operators
  • Python Lists
Powered by GitBook
On this page
  1. Python Strings

Python - Escape Characters

In Python, you can use escape characters to insert special characters or symbols into strings. Escape characters are preceded by a backslash (\) and are used to represent characters that are either illegal or have special meanings within strings.

Common Escape Characters

Here are some common escape characters used in Python:

  • \': Represents a single quote.

  • \\: Represents a backslash.

  • : Represents a new line.

  • : Represents a carriage return.

  • : Represents a tab.

  • \b: Represents a backspace.

  • \f: Represents a form feed.

  • \ooo: Represents an octal value (replace "ooo" with the octal value).

  • \xhh: Represents a hex value (replace "hh" with the hex value).

Example

txt = "This is a single quote: \' and this is a new line:\nSee the tab\tand backslash: \\."
print(txt)

In this example, escape characters are used to include a single quote, a new line, a tab, and a backslash within the string.

Escape characters are essential when you need to include special characters or formatting within your strings. They allow you to include characters that might otherwise be interpreted differently.

PreviousPython - Format StringsNextPython - String Methods

Last updated 1 year ago