{"id":3577,"date":"2022-12-01T20:01:58","date_gmt":"2022-12-01T20:01:58","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3577"},"modified":"2022-12-01T20:02:24","modified_gmt":"2022-12-01T20:02:24","slug":"python-escape-sequence","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-escape-sequence\/","title":{"rendered":"Python Escape Sequence With Example"},"content":{"rendered":"\n<p class=\"has-text-align-center\">Python Escape Sequence<\/p>\n\n\n\n<p>Basically, when we are writing strings, we might come across some situations, where we need to insert something into the string, which we cannot do directly, or if we are required to format our string in some way, we would want to make use of the escape sequences. The thing is that when we would have a look at the examples, we can then understand what could be the need for using the escape sequences. Basically, Let\u2019s consider an example now, where we are trying to get a new line into the string. If we have our string surrounded by double or single quotes, and we try to hit enter in order to get the new line, we would simply run into errors. <\/p>\n\n\n\n<p>Have a look at this &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"75\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE.jpg\" alt=\"\" class=\"wp-image-3578\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE.jpg 639w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-300x35.jpg 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Python Escape Sequence<\/h2>\n\n\n\n<p>As you can see, within the string, we are trying to get a new line, between GyaniPandit and Python, but we are not able to get it like that. So, in such a situation, we can make use of the escape sequence as well. Using an escape sequence is like you are going to use something in your string, that you may not be able to use in some other (normal) situations. So, we are going to make use of the backslash in this case.<\/p>\n\n\n\n<p>So, correcting the above program, we need to add a new line after GyaniPandit, which we are going to achieve with the help of an escape sequence. So, the escape sequence that we are going to use here, is the &#8216;\\n&#8217;, which would simply mean a new line. Have a look at the below program.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"72\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-1.jpg\" alt=\"\" class=\"wp-image-3579\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-1.jpg 665w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-1-300x32.jpg 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, in the above program, we have used the escape sequence \\n, which simply means that we are adding the new line. Also, we have some other escape sequences for doing different things here.&nbsp; Let\u2019s have a look at some of the escape sequences. Here are some escape sequences in the given table, and also we are going to take some examples related to them so that we can understand those escape sequences.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Escape Sequence<\/td><td>Meaning<\/td><\/tr><\/thead><tbody><tr><td>\\n<\/td><td>Adds a new line<\/td><\/tr><tr><td>\\t<\/td><td>Adds a tab<\/td><\/tr><tr><td>\\\\<\/td><td>Adds a backslash<\/td><\/tr><tr><td>\\&#8217;<\/td><td>Adds a single quote<\/td><\/tr><tr><td>\\&#8221;<\/td><td>Adds a double quote<\/td><\/tr><tr><td>\\b<\/td><td>Adds a backspace<\/td><\/tr><tr><td>\\xhh<\/td><td>A character with the hex value hh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Well, in the above table, we have some escape sequences, that can be used from time to time. Let\u2019s have a look at some examples, through which, we can simply understand those escape sequences.<\/p>\n\n\n\n<p>First of all, we have seen the \\n thing here, which adds a new line to our string. You can try in furthermore examples, but since we have used it previously, Let\u2019s move forward to another escape sequence, which is \\t, which adds up the tab in the string. Let\u2019s have a look at an example, through which we can understand the \\t escape sequence.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"97\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-2.jpg\" alt=\"\" class=\"wp-image-3580\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-2.jpg 644w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-2-300x45.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, we have used the escape sequence \\t between Gyani and Pandit. So, when we are printing the string, we would simply see that there are some spaces added between Gyani and Pandit.<\/p>\n\n\n\n<p>So, whenever you need to give some tab in the string, you can make use of the \\t escape sequence. Again, this is not the thing that you can only use the escape sequence for once. You can use it as many times as you wish, something like this &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"94\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-3.jpg\" alt=\"\" class=\"wp-image-3581\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-3.jpg 645w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-3-300x44.jpg 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n<\/div>\n\n\n<p>I know this does not make sense here, that we are using so many tabs over here, but still, if you want to use it somewhere in the string, you can use it.<\/p>\n\n\n\n<p>Now, Let\u2019s try to use some other escape characters. Let\u2019s say that if we are required to make use of some double quotes, inside our string, we have surrounded our string with the double quotes. So, we would not be able to use the double quotes directly into the program. Have a look at this thing &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"88\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-4.jpg\" alt=\"\" class=\"wp-image-3582\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-4.jpg 634w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-4-300x42.jpg 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, in the above program, we have surrounded the string with the double quotes, so the double quotes are supposed to mark the start and the end of the string. But here, we wanted the string GyaniPandit into double quotes, so we also surrounded that string GyaniPandit into double quotes. But this didn&#8217;t work out in the way that it was supposed to. Instead, we ran into errors.<\/p>\n\n\n\n<p>So, the thing is that we are not directly allowed to use the double quotes, inside a string, which is surrounded by double quotes. In such a case, we are required to use the escape sequence for the same. Have a look at the changes that we have made &#8211;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"73\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-5.jpg\" alt=\"\" class=\"wp-image-3583\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-5.jpg 644w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-5-300x34.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, now before the double quotes which are surrounding the string GyaniPandit, we have put the backslash, which means that we can now use the double quotes here, and we are not going to run into any errors. So, if you try to run the program, you will simply get to see the string GyaniPandit surrounded with double quotes.<\/p>\n\n\n\n<p>Also, if we do not want to use the escape character here, we can do some alternate things. Like if we know that we are going to use double quotes inside the string, we can surround the string with single quotes. Have a look at the program, which tries to demonstrate the same thing.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"88\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-6.jpg\" alt=\"\" class=\"wp-image-3584\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-6.jpg 650w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-6-300x41.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, we could use the double quotes freely, without using the escape sequence, when the string was surrounded by the single quotes. Even the opposite thing is true, which means that if the string is surrounded by double quotes, and you want to use the single quotes in the string, you can simply use the single quotes there. On the other hand, if you are using the single quotes, inside a string, which is surrounded by single quotes, you would have to make use of the escape sequence.<\/p>\n\n\n\n<p>Let\u2019s have a look at an example, to understand this as well.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"94\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-7.jpg\" alt=\"\" class=\"wp-image-3585\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-7.jpg 650w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/ESCAPE-SEQUENCE-7-300x43.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, the string in the above program is surrounded by single quotes, and in the string as well, we are trying to use single quotes. We know that we are not allowed to do this directly, so we are making use of the escape sequence (\\&#8217;). Alternatively, you can surround the string with the double quotes, and then use the single quotes in the string, without any escape sequence.<\/p>\n\n\n\n<p>So, using the escape sequence becomes important sometimes, when we are required to use something that we would not be allowed to use otherwise.<\/p>\n\n\n\n<p>You can also try some other escape sequences as well, from the table. When we get to use them in the programs, we would get comfortable using them and become familiar with some of the different escape sequences. You can also explore some more escape sequences as well, while we would move to our next concept.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Escape Sequence Basically, when we are writing strings, we might come across some situations, where we need to insert something into the string, which we cannot do directly, or if we are required to format our string in some way, we would want to make use of the escape sequences. The thing is that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3577","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3577"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3577\/revisions"}],"predecessor-version":[{"id":4163,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3577\/revisions\/4163"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}