Escape Character for strings in Python by Manish Sharma

One thing the last tutorial made very clear to us is that, if a string contains punctuation marks like double quotes in the middle, enclose it into a pair of single quotes. Similarly, if a string contains a punctuation mark like apostrophe then wrap it into a pair of double quotes. But what if we don’t want to switch between quotation marks again and again. Let’s learn how to handle punctuation marks of a string using Escape character in Python Programming.

Switching between quotation marks to display a character string is nothing but pain and to deal with it, Python provides us with an option that is “Escape Character”.

What is an Escape Character?

An escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence.  ~Wikipedia

Python Interpreter uses Backward Slash or Backslash (\) as an escape character. You can use this escape character to deal with special punctuation marks which come in the middle of the string that you want to process.

How to use Escape Character?

Using escape character to handle punctuation marks in python programming is very easy. You simply have to put the backslash before the punctuation mark that you want to keep intact in your string.

     I\’m Manish from RebellionRider

Example of Escape Character in Python Programming.

To better understand it let’s do some examples. For the demonstration let us use the same strings which we used in the previous tutorial. These were:

String 1. What’s up Internet!

String 2. I am going to be a “Python Programmer” very soon.

Example 1: What’s up Internet!

The string what’s up internet! Contains an apostrophe mark ( ’ ) in the middle thus as per the rule, we must enclose this into a pair of double quotes so that Python Interpreter can process it without raising any errors.  

Let’s say you don’t want to do that, you want to use single quotes. But that will conflict with apostrophe mark and raise an error.

Escape Character in Python programming Language by manish sharma

In such situations, we can use escape character to solve the above-shown errors, like this.

print (‘what\’s up Internet!’)

You simply have to put the back slash right before the punctuation mark that you want to keep intact in your string. In this case that punctuation mark was the apostrophe sign (What’s).

Example 2: I am going to be a “Python Programmer” very soon.

Unlike previous string this one has multiple punctuation marks which needs to get processed as they are appearing in the actual strings. As the string contains double quotes in the middle thus it needs to be enclosed inside the pair of single quotes in order to get printed correctly like this

single line strings in python programming by manish sharma

But in case, if you don’t want to use single quotes or you have accidently enclosed your string into a pair of double quotes in your project then you take help of the escape character. Like this

print (“I am going to be a \“Python Programmer\” very soon.”);

You simply have to put the back slash before both the quotation marks in your string and that will solve your error.

Quotation marks for enclosing the string doesn’t matter, as long as you are using the escape character. Click To Tweet

Conclusion.

Use of escape character minimises the need of switching quotation marks for enclosing the strings, which contains special punctuation marks. Therefore, you can use whatever quotation marks to enclose your string and bypass the grammatical punctuation that comes in the middle by using escape character (\) in Python Programming language.

Do make sure to subscribe to my YouTube channel for more interesting Python programming Videos.

2 COMMENTS