{"id":3235,"date":"2021-12-04T00:56:29","date_gmt":"2021-12-04T00:56:29","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3235"},"modified":"2025-03-30T08:20:48","modified_gmt":"2025-03-30T08:20:48","slug":"python-backslash","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-backslash\/","title":{"rendered":"Python Backslash"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python backslash character as a part of a special sequence character or to escape characters in a string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-backslash'>Introduction to the Python backslash <a href=\"#introduction-to-the-python-backslash\" class=\"anchor\" id=\"introduction-to-the-python-backslash\" title=\"Anchor for Introduction to the Python backslash\">#<\/a><\/h2>\n\n\n\n<p>In Python, the backslash(<code>\\<\/code>) is a special character. If you use the backslash in front of another character, it changes the meaning of that character.<\/p>\n\n\n\n<p>For example, the <code>t<\/code> is a literal character. But if you use the backslash character in front of the letter <code>t<\/code>, it\u2019ll become the tab character (<code>\\t<\/code>).<\/p>\n\n\n\n<p>Generally, the backslash has two main purposes.<\/p>\n\n\n\n<p>First, the backslash character is a part of special character sequences such as the tab character <code>\\t<\/code> or the new line character <code>\\n<\/code>.<\/p>\n\n\n\n<p>The following example prints a string that has a newline character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Hello,\\n World'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cHJpbnQoJ0hlbGxvLFxuIFdvcmxkJyk\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hello,\nWorld<\/code><\/span><\/pre>\n\n\n<p>The \\n is a single character, not two. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s = <span class=\"hljs-string\">'\\n'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(len(s)) <span class=\"hljs-comment\"># 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cyA9ICdcbicNCnByaW50KGxlbihzKSkgIyAx\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, the backslash (<code>\\<\/code>) escape other special characters. For example, if you have a string that has a single quote inside a single-quoted string like the following string, you need to use the backslash to escape the single quote character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s = <span class=\"hljs-string\">'\"Python\\'s awesome\" She said'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cyA9ICciUHl0aG9uXCdzIGF3ZXNvbWUiIFNoZSBzYWlkJw0KcHJpbnQocyk\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">\"Python's awesome\"<\/span> She said<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='backslash-in-f-strings'>Backslash in f-strings <a href=\"#backslash-in-f-strings\" class=\"anchor\" id=\"backslash-in-f-strings\" title=\"Anchor for Backslash in f-strings\">#<\/a><\/h2>\n\n\n\n<p>PEP-498 specifies that an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-f-strings\/\">f-string<\/a> cannot contain a backslash character as a part of the expression inside the curly braces <code>{}<\/code>. <\/p>\n\n\n\n<p>The following example will result in an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>,<span class=\"hljs-string\">'green'<\/span>,<span class=\"hljs-string\">'blue'<\/span>]\ns = f<span class=\"hljs-string\">'The RGB colors are:\\n {'<\/span>\\n<span class=\"hljs-string\">'.join(colors)}'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCdncmVlbicsJ2JsdWUnXQ0KcyA9IGYnVGhlIFJHQiBjb2xvcnMgYXJlOlxuIHsnXG4nLmpvaW4oY29sb3JzKX0nDQpwcmludChzKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">SyntaxError<\/span>: f-string expression part cannot include a backslash<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To fix this, you need to join the strings in the <code>colors<\/code> list before placing them in the curly braces:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">colors = &#91;<span class=\"hljs-string\">'red'<\/span>,<span class=\"hljs-string\">'green'<\/span>,<span class=\"hljs-string\">'blue'<\/span>]\nrgb = <span class=\"hljs-string\">'\\n'<\/span>.join(colors)\ns = f<span class=\"hljs-string\">\"The RGB colors are:\\n{rgb}\"<\/span>\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y29sb3JzID0gWydyZWQnLCdncmVlbicsJ2JsdWUnXQ0KcmdiID0gJ1xuJy5qb2luKGNvbG9ycykNCnMgPSBmIlRoZSBSR0IgY29sb3JzIGFyZTpcbntyZ2J9Ig0KcHJpbnQocyk\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">The RGB colors are:\nred\ngreen\nblue<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='backslash-in-raw-strings'>Backslash in raw strings <a href=\"#backslash-in-raw-strings\" class=\"anchor\" id=\"backslash-in-raw-strings\" title=\"Anchor for Backslash in raw strings\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-raw-strings\/\">Raw strings<\/a> treat the backslash character (<code>\\<\/code>) as a literal character. The following example treats the backslash character <code>\\<\/code> as a literal character, not a special character:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">s = r<span class=\"hljs-string\">'\\n'<\/span>\n<span class=\"hljs-keyword\">print<\/span>(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cyA9IHInXG4nDQpwcmludChzKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">\\n<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The python backslash character (<code>\\<\/code>) is a special character used as a part of a special sequence such as <code>\\t<\/code> and <code>\\n<\/code>.<\/li>\n\n\n\n<li>Use the Python backslash (<code>\\<\/code>) to escape other special characters in a string.<\/li>\n\n\n\n<li>F-strings cannot contain the backslash a part of expression inside the curly braces <code>{}<\/code>.<\/li>\n\n\n\n<li>Raw strings treat the backslash (\\) as a literal character.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=backslash\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3235\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-backslash\/\"\n\t\t\t\tdata-post-title=\"Python Backslash\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3235\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-backslash\/\"\n\t\t\t\tdata-post-title=\"Python Backslash\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you\u2019ll learn about the Python backslash character as a part of a special sequence character or to escape characters in a string.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":77,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3235","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3235"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3235\/revisions"}],"predecessor-version":[{"id":7202,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3235\/revisions\/7202"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}