How to Print Backslash in Python

In this tutorial, you will learn how to print Backslash in Python.

In Python, the backslash ( \ ) character is used for Escape Sequence. This means that the backslash character is used to escape characters that have special meaning like a newline or quotes.

Print Backslash or escape from backslash in Python

In python \n, \t, \v and many more have special meanings.
\n is used to print a new line, \t is used to print a tab space and \v is used as vertical space.

Let us understand this from examples.

 

  • \n
print("Welcome to\n Code Speedy")

Output:

Welcome to 
 Code Speedy
  • \t
print("Welcome to \tCode Speedy")

Output:

Welcome to      Code Speedy
  • \v
print("Welcome to \vCode Speedy")

Output:

Welcome to                                                                                                                                                                   
           Code Speedy

Escape Backslash in Python

Then the question arises on how to print backslash (\) in python. We just cannot print backslash (\) along with another character, for example, n is just a character but with a backslash (\) in its precedence, it will become a newline character (\n) similarly t with \ as precedence will become \t which is tab space.

The solution to this problem is by escaping \ with another backslash ie \\.

print("\\")

OUTPUT:

\

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *