{"id":12434,"date":"2021-02-10T05:04:40","date_gmt":"2021-02-10T05:04:40","guid":{"rendered":"https:\/\/www.askpython.com\/?p=12434"},"modified":"2022-03-23T05:36:31","modified_gmt":"2022-03-23T05:36:31","slug":"difference-between-single-and-double-quotes-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/difference-between-single-and-double-quotes-in-python","title":{"rendered":"Difference Between Single and Double Quotes in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <a href=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\" class=\"rank-math-link\">String<\/a> is a sequence of characters. You are allowed to <strong>start<\/strong> and <strong>end<\/strong> a&nbsp;string&nbsp;literal with single and double quotes in Python. There are two ways to represent a string in python programming. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, you will see the difference between both the quotation marks with the help of an example i.e. code with its output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are single quotes used for in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Single quotes are used to mark a quote within a quote or a direct quote in a news story headline. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When programming with Python, we generally use single quotes for string literals. For example &#8211; <em>&#8216;my-identifier&#8217;<\/em>. Let us understand with an example through code in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NOTE:<\/strong> Always make use of single quotes when you know your string may contain double quotes within.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example usage of single quotes in Python<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Below is the code where you can see the implementation of single quote.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nword = &#039;Ask?&#039;\nprint(word)\nsentence = &#039;Python Programming&#039;\nprint(sentence)\nname = &#039;&quot;Hi&quot; ABC&#039;\nprint(name)\ncongrat = &#039;We congrat&#039;s you.&#039;\nprint(congrat)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nAsk?\nPython Programming\n&quot;Hi&quot; ABC\nInvalid Syntax\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What are double quotes in Python used for?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A double quotation mark is&nbsp;to set off a direct (word-for-word)&nbsp;quotation. For example &#8211; &#8220;I hope you will be here,&#8221; he said. In Python Programming, we use Double Quotes for string representation. Let us understand with an example through code in python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NOTE:<\/strong> Use double quotes to enclose your strings when you know there are going to be single quotes within your string<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nwish = &quot;Hello World!&quot;\nprint(wish)\nhey = &quot;AskPython says &quot;Hi&quot;&quot;\nprint(hey)\nfamous =&quot;&#039;Taj Mahal&#039; is in Agra.&quot;\nprint(famous)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHello World!\nInvalid Syntax\n&#039;Taj Mahal&#039; is in Agra.\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Key Differences Between Single and Double Quotes in Python<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Single Quotation Mark<\/strong><\/td><td><strong>Double Quotation Mark<\/strong><\/td><\/tr><tr><td>Represented as &#8216; &#8216;<\/td><td>Represented as &#8221; &#8220;<\/td><\/tr><tr><td>Single quotes for anything that behaves like an Identifier.<\/td><td>Double quotes generally we used for text.<\/td><\/tr><tr><td>Single quotes are used for regular expressions, dict keys or SQL.<\/td><td>Double quotes are used for string representation.<\/td><\/tr><tr><td>Eg. &#8216;We &#8220;welcome&#8221; you.&#8217;<\/td><td>Eg. &#8220;Hello it&#8217;s me.&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus &#8211; Triple Quotes in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">What if you have to use strings that may include both single and double quotes? For this, Python allows you to use triple quotes. A simple example for the same is shown below. Triple quotes also allow you to add multi-line strings to Python variables instead of being limited to single lines. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of triple quotes<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsentence1 = &#039;&#039;&#039;He asked, &quot;did you speak with him?&quot;&#039;&#039;&#039;\nprint(sentence1)\nsentence2 = &#039;&#039;&#039;&quot;That&#039;s great&quot;, she said.&#039;&#039;&#039;\nprint(sentence2)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nHe asked, &quot;did you speak with him?&quot;\n&quot;That&#039;s great&quot;, she said.\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As you can see, Python now understands that the double and single quotes are part of the string and do not need to be escaped.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude this simple topic, I&#8217;d like to say this &#8211; the difference between single and double quotes in Python is not huge. It absolutely depends on the circumstances that we use single and double quotes in. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a programmer, you can decide what fits best for your string declaration. And when in doubt, go for the triple quotes so you have no issues with what&#8217;s included within the string. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A String is a sequence of characters. You are allowed to start and end a&nbsp;string&nbsp;literal with single and double quotes in Python. There are two ways to represent a string in python programming. In this article, you will see the difference between both the quotation marks with the help of an example i.e. code with [&hellip;]<\/p>\n","protected":false},"author":23,"featured_media":12435,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-12434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=12434"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12434\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/12435"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=12434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=12434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=12434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}