{"id":185,"date":"1997-06-30T10:52:26","date_gmt":"1997-06-30T10:52:26","guid":{"rendered":"http:\/\/python-tutorials.com\/?p=185"},"modified":"2022-01-29T02:24:20","modified_gmt":"2022-01-29T01:24:20","slug":"strings","status":"publish","type":"post","link":"https:\/\/geek-university.com\/python\/strings\/","title":{"rendered":"Strings"},"content":{"rendered":"<p><span style=\"font-size: 12pt; color: #313b3d;\"><strong>Strings<\/strong>\u00a0in Python can be\u00a0used to store a contiguous set of characters. To define a string, you simple type the characters within single or double quotes. For example, <em>newString = &#8216;Hello world!&#8217;<\/em> will store that string of characters (<em>Hello world!<\/em>) in the variable called <em>newString<\/em>:<\/span><\/p>\n<pre class=\"nums:false wrap-toggle:false copy:false lang:python decode:true\">&gt;&gt;&gt; newString = 'Hello world!'\r\n&gt;&gt;&gt; print (newString)\r\nHello world!<\/pre>\n<p><span style=\"font-size: 12pt; color: #313b3d;\">You will get the same result if you use the double quotes to define a string:<\/span><\/p>\n<pre class=\"nums:false wrap-toggle:false copy:false lang:python decode:true\">&gt;&gt;&gt; newString = \"Hello world!\"\r\n&gt;&gt;&gt; print(newString)\r\nHello world!<\/pre>\n<p><span style=\"font-size: 12pt; color: #313b3d;\">The reason why both forms are used is so you can embed a quote character of the other type inside a string. For example, you may embed a single-quote character in a string enclosed in double-quote characters:<\/span><\/p>\n<pre class=\"nums:false wrap-toggle:false copy:false lang:python decode:true\">&gt;&gt;&gt; newString = \"Mark's car\"\r\n&gt;&gt;&gt; print(newString)\r\nMark's car<\/pre>\n<p><span style=\"font-size: 12pt; color: #313b3d;\">Note that you can not perform numeric operations with string variables, even if the string stored in the variable consists only of numbers. Consider the following example:<\/span><\/p>\n<pre class=\"nums:false wrap-toggle:false copy:false lang:python decode:true\">&gt;&gt;&gt; x = '5'\r\n&gt;&gt;&gt; y = '3'\r\n&gt;&gt;&gt; print(x + y)\r\n53<\/pre>\n<p><span style=\"font-size: 12pt; color: #313b3d;\">The <strong>+<\/strong> operator concatenates two strings. To perform an arithmetic operation, we need to convert the string variables to integer or floating-point variables:<\/span><\/p>\n<pre class=\"nums:false wrap-toggle:false copy:false lang:python decode:true\">&gt;&gt;&gt; x = int('5')\r\n&gt;&gt;&gt; y = int('3')\r\n&gt;&gt;&gt; print(x + y)\r\n8<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Strings\u00a0in Python can be\u00a0used to store a contiguous set of characters. To define a string, you simple type the characters &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[840],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":4,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":13496,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/posts\/185\/revisions\/13496"}],"wp:attachment":[{"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geek-university.com\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}