Python Escape Characters: Special Text Handling in Python

thumb_up 1  ·  sell Python escape characters, Escaping text in Python, Handling special characters in Python strings

In Python, a string becomes a raw string if it is prefixed with "r" or "R" before the quotation symbols. Hence 'Hello' is a normal string whereas r'Hello' is a raw string.

>>> normal="Hello" >>> print (normal) Hello >>> raw=r"Hello" >>> print (raw) Hello

In normal circumstances, there is no difference between the two. However, when the escape character is embedded in the string, the normal string actually interprets the escape sequence, whereas the raw string doesn't process the escape character.

>>> normal="Hello\nWorld" >>> print (normal) Hello World >>> raw=r"Hello\nWorld" >>> print (raw) Hello\nWorld

In the above example, when a normal string is printed the escape character '\n' is processed to introduce a newline. However, because of the raw string operator 'r' the effect of escape character is not translated as per its meaning.

The newline character \n is one of the escape sequences identified by Python. Escape sequence invokes an alternative implementation character subsequence to "\". In Python, "\" is used as escape character. Following table shows list of escape sequences.

Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are −

Sr.No Escape Sequence & Meaning
1

\<newline>

Backslash and newline ignored

2

\\

Backslash (\)

3

\'

Single quote (')

4

\"

Double quote (")

5

\a

ASCII Bell (BEL)

6

\b

ASCII Backspace (BS)

7

\f

ASCII Formfeed (FF)

8

\n

ASCII Linefeed (LF)

9

\r

ASCII Carriage Return (CR)

10

\t

ASCII Horizontal Tab (TAB)

11

\v

ASCII Vertical Tab (VT)

12

\ooo

Character with octal value ooo

13

\xhh

Character with hex value hh

Example

The following code shows the usage of escape sequences listed in the above table −

 
# ignore \ s = 'This string will not include \ backslashes or newline characters.' print (s) # escape backslash s=s = 'The \\character is called backslash' print (s) # escape single quote s='Hello \'Python\'' print (s) # escape double quote s="Hello \"Python\"" print (s) # escape \b to generate ASCII backspace s='Hel\blo' print (s) # ASCII Bell character s='Hello\a' print (s) # newline s='Hello\nPython' print (s) # Horizontal tab s='Hello\tPython' print (s) # form feed s= "hello\fworld" print (s) # Octal notation s="\101" print(s) # Hexadecimal notation s="\x41" print (s)

It will produce the following output −

This string will not include backslashes or newline characters.
The \character is called backslash
Hello 'Python'
Hello "Python"
Helo
Hello
Hello
Python
Hello Python
hello
world
A
A




The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back