{"id":17,"date":"2022-08-23T07:31:39","date_gmt":"2022-08-23T07:31:39","guid":{"rendered":"https:\/\/python.freelinuxtutorials.com\/?p=17"},"modified":"2022-08-23T07:31:39","modified_gmt":"2022-08-23T07:31:39","slug":"pythondata-typestrings","status":"publish","type":"post","link":"https:\/\/python.freelinuxtutorials.com\/index.php\/2022\/08\/23\/pythondata-typestrings\/","title":{"rendered":"Python>>Data Type:Strings"},"content":{"rendered":"<p>Strings are text type and are sequences of character data. It is represented by quotation marks, either single or double quotes.<\/p>\n<p>Example:<\/p>\n<p>&#8220;Hello&#8221;<\/p>\n<p>&#8216;What is your name?&#8217;<\/p>\n<blockquote><p>&gt;&gt;&gt; &#8220;Hello&#8221;<br \/>\n&#8216;Hello&#8217;<br \/>\n&gt;&gt;&gt; x = &#8216;What is your name?&#8217;<br \/>\n&gt;&gt;&gt; print (x)<br \/>\nWhat is your name?<br \/>\n&gt;&gt;&gt;<\/p><\/blockquote>\n<p>Some key notes:<\/p>\n<p>1.Escape sequences in strings = use backslash (\\) character<\/p>\n<div>Examples:<\/div>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>&gt;&gt;&gt; print (&#8216;This is a single quote symbol \\&#8217; &#8216;)<\/div>\n<\/blockquote>\n<div>\n<blockquote>\n<div>This is a single quote symbol &#8216;<\/div>\n<div>&gt;&gt;&gt; print (&#8216;This is a single quote symbol (\\&#8217;)&#8217;)<\/div>\n<div>This is a single quote symbol (&#8216;)<\/div>\n<div>&gt;&gt;&gt; print (&#8220;This is a single quote symbol (\\&#8217;)&#8221;)<\/div>\n<div>This is a single quote symbol (&#8216;)<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; print (&#8216;I&#8217;m handsome&#8217;)<\/div>\n<div>\u00a0 File &#8220;&lt;stdin&gt;&#8221;, line 1<\/div>\n<div>\u00a0 \u00a0 print (&#8216;I&#8217;m handsome&#8217;)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ^<\/div>\n<div>SyntaxError: invalid syntax<\/div>\n<div>&gt;&gt;&gt; print (&#8216;I\\&#8217;m handsome&#8217;)<\/div>\n<div>I&#8217;m handsome<\/div>\n<\/blockquote>\n<\/div>\n<blockquote>\n<div><\/div>\n<div>\n<div>&gt;&gt;&gt; x = &#8220;I am the \\&#8221;King\\&#8221; of this land&#8221;<\/div>\n<div>&gt;&gt;&gt; print (x)<\/div>\n<div>I am the &#8220;King&#8221; of this land<\/div>\n<\/div>\n<div><\/div>\n<\/blockquote>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; print (&#8216;This is a backslash symbol \\\\&#8217;)<\/div>\n<div>This is a backslash symbol \\<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div>Other popular escape characters:<br \/>\n\\n = new line<\/div>\n<div>\\t = tab<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; print (&#8216;test \\n only&#8217;)<\/div>\n<div>test<\/div>\n<div>\u00a0only<\/div>\n<\/blockquote>\n<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; print (&#8216;test \\t only&#8217;)<\/div>\n<div>test\u00a0 \u00a0 \u00a0only<\/div>\n<\/blockquote>\n<\/div>\n<blockquote>\n<div><\/div>\n<\/blockquote>\n<p>2. Since it is a sequence, can use indexing and slicing for the subsets of strings using the slice operator\u00a0\u00a0([ ] and [:] ). Indexes starting &#8220;0&#8221; in the start of the string.<\/p>\n<div><\/div>\n<div>Example:<\/div>\n<div>Character: E\u00a0 u\u00a0 a\u00a0 n<\/div>\n<div>Index:\u00a0 \u00a0 \u00a0 \u00a0 0\u00a0 1\u00a0 2\u00a0 3<\/div>\n<div>Neg Index:-4 -3 -2 -1<\/div>\n<blockquote>\n<div><\/div>\n<div>&gt;&gt;&gt; x = &#8220;Euan&#8221;<br \/>\n&gt;&gt;&gt; print(x[0])<br \/>\nE<br \/>\n&gt;&gt;&gt; print(x[3])<br \/>\nn<br \/>\n&gt;&gt;&gt; print(x[-3])<br \/>\nu<br \/>\n&gt;&gt;&gt; print(x[-4])<br \/>\nE<\/div>\n<\/blockquote>\n<div>For slicing, it lets you access parts of the sequence, normally we use it if we want to get the part of the string and not the complete string.<\/div>\n<div>Syntax:<\/div>\n<div>string[start : end : step]<\/div>\n<div>where:<\/div>\n<div>start = starting index<\/div>\n<div>end = end index (not included in substring,<span style=\"text-decoration: underline;\">go up to but don&#8217;t include it<\/span>)<\/div>\n<div>step=optional argument determining the increment between each index for slicing<\/div>\n<div>Example:<\/div>\n<div>W r\u00a0 i\u00a0 t\u00a0 e c o d e<\/div>\n<div>\u00a00 1 2 3 4\u00a0 5 6 7 8<\/div>\n<div><\/div>\n<div>You only want to get the word code in this string (remember the indexing concept<\/div>\n<blockquote><p>&gt;&gt;&gt; x =&#8221;Writecode&#8221;<br \/>\n&gt;&gt;&gt; print(x)<br \/>\nWritecode<br \/>\n&gt;&gt;&gt; print (x[5:])<br \/>\ncode<\/p><\/blockquote>\n<div><\/div>\n<div>So if you want to get the word &#8220;Write&#8221;, then you can do this way<\/div>\n<div><\/div>\n<blockquote>\n<div>&gt;&gt;&gt; print (x[:5])<br \/>\nWrite<\/div>\n<\/blockquote>\n<div><\/div>\n<div>How about middle?example we want to take word &#8220;teco&#8221;<\/div>\n<div><\/div>\n<blockquote>\n<div>&gt;&gt;&gt; print (x[3:7])<br \/>\nteco<\/div>\n<\/blockquote>\n<div><\/div>\n<div>More indexing and slicing for next post.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Strings are text type and are sequences of character data. It is represented by quotation marks, either single or double quotes. Example: &#8220;Hello&#8221; &#8216;What is your name?&#8217; &gt;&gt;&gt; &#8220;Hello&#8221; &#8216;Hello&#8217; &gt;&gt;&gt; x = &#8216;What is your name?&#8217; &gt;&gt;&gt; print (x) What is your name? &gt;&gt;&gt; Some key notes: 1.Escape sequences in strings = use backslash&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[4,6],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-data-types","tag-data-type","tag-strings"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":1,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}