{"id":2930,"date":"2018-03-04T11:51:16","date_gmt":"2018-03-04T11:51:16","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2930"},"modified":"2018-03-08T14:30:40","modified_gmt":"2018-03-08T14:30:40","slug":"python-2-7-fundamentals-collections-strings","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/04\/python-2-7-fundamentals-collections-strings\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Strings"},"content":{"rendered":"<p>A String is a collection of characters. What are characters? In simple terms, all the letters, numbers and symbols that you find on a keyboard are each called a character. So A 9 &amp; are examples of three characters. \u00a0A space is also a character. When you group characters together, it is called a String. Strings in Python are written surrounded by single quotes or double quotes.<\/p>\n<p>Eg. &#8216;cat&#8217; or &#8220;cat&#8221;<\/p>\n<p>If you try to use strings without the surrounding quotes, you will get an error. You can also have an empty string &#8211; a string without any characters. Eg. &#8220;&#8221; or &#8221; . The length of a string is the number of characters in the string. Strings are immutable i.e once you declare a string variable you cannot change its value without creating another string.<\/p>\n<h3><strong>Operators<\/strong><\/h3>\n<p>The following operators are available for strings:<\/p>\n<ol>\n<li>[] &#8211; index operator.<\/li>\n<li>[:] \u00a0&#8211; slicing<\/li>\n<li>\u00a0+ adding<\/li>\n<\/ol>\n<p>We will look at the above in more detail.<\/p>\n<p><strong>[] &#8211; index operator<\/strong><\/p>\n<p>As mentioned before, strings are collections of characters. So to access individual characters in a string we use the index operator. The lowest index value is zero which represents the first character and the highest index value is the length of the string minus one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2995\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string1.png\" alt=\"\" width=\"618\" height=\"266\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string1.png 618w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string1-300x129.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see above print name[13] gives an error since the length of the string is 12. For an empty string blank = &#8221; the length of the string is zero, so print blank[0] gives an error.<\/p>\n<p><strong>[:] slicing<\/strong><\/p>\n<p>The index operator lets you extract one character from the string. The slicing operator lets us extract multiple characters from a string. The operator takes two numbers [x:y] where x is the starting index and y is the ending index. Note that the ending index is not inclusive eg. if you try print name[1:3] it will extract character at index 1 and then stop at index 2.<\/p>\n<p>The slicing operator has various permutations and combinations as shown below:<\/p>\n<ul>\n<li>[x:y] &#8211; extract from x and stop before y<\/li>\n<li>[x:] &#8211; extract from x till the rest of the string<\/li>\n<li>[:y] &#8211; extract from the first character till y<\/li>\n<li>[-x:] &#8211; extract x characters from the end<\/li>\n<li>[:-y] &#8211; extract from the first character and stop before y<\/li>\n<li>[:] &#8211; no x and y . Extract the whole string<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2999\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string2.png\" alt=\"\" width=\"617\" height=\"227\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string2.png 617w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string2-300x110.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>+ adding<\/strong><\/p>\n<p>Adding two strings together gives a new third string. Examples are shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3000\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string3.png\" alt=\"\" width=\"610\" height=\"198\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string3.png 610w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string3-300x97.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Methods<\/h3>\n<p>Methods are functions which are available for string variables. The following methods are available:<\/p>\n<ul>\n<li>lower() &#8211; convert string to lowercase<\/li>\n<li>upper() &#8211; convert string to uppercase<\/li>\n<li>strip() \u00a0&#8211; remove any leading or trailing spaces in the string<\/li>\n<li>isalpha() &#8211; check if all characters in string are alphabets<\/li>\n<li>isdigit() &#8211; check if all characters in string are digits<\/li>\n<li>isspace() &#8211; check if all characters in string are spaces<\/li>\n<li>startswith(x) &#8211; check if string starts with the string specified by x<\/li>\n<li>endswith(x) &#8211; check if string ends with the string specified by x<\/li>\n<li>find(x) &#8211; checks if string contains string specified by x and returns the index if found else returns -1<\/li>\n<li>replace(x, y) &#8211; replaces all occurrences of string x with string y<\/li>\n<li>split(x) &#8211; splits a string into a list using the delimiter specified by string x<\/li>\n<li>join(x) &#8211; reverse of split() . Converts separate strings in a list into a string using this string as a delimiter<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3004\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string4.png\" alt=\"\" width=\"615\" height=\"561\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string4.png 615w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/string4-300x274.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The startswith() and endswith() methods are case-sensitive as are the other methods. The split() and join() methods deal with lists which we will read about in the next posts.<\/p>\n<p>There are other string methods, but the ones mentioned above are the basic methods which cover all the possible operations on strings.<\/p>\n<p>In the next post we look at <a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/08\/python-2-7-fundamentals-collections-lists\/\" target=\"_blank\" rel=\"noopener\">lists<\/a> .<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A String is a collection of characters. What are characters? In simple terms, all the letters, numbers and symbols that you find on a keyboard <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/04\/python-2-7-fundamentals-collections-strings\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Strings\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2930","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2930"}],"version-history":[{"count":12,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2930\/revisions"}],"predecessor-version":[{"id":3029,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2930\/revisions\/3029"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}