{"id":4246,"date":"2022-03-09T16:52:15","date_gmt":"2022-03-09T16:52:15","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4246"},"modified":"2025-01-23T13:36:45","modified_gmt":"2025-01-23T13:36:45","slug":"using-strings-extracting","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/strings-python\/using-strings-extracting","title":{"rendered":"Using Strings-Extracting"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Substring<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>result = my_string&#91;first_index:last_index_plus_one]\n\nresult = my_string&#91;3:6]    \/\/Get the characters from index 3 up to index 5 (the last index given is not included)\n\nresult = my_string&#91;:6]    \/\/Get all the characters up to index 5 (the last index given is not included)\n\nresult = my_string&#91;3:]    \/\/Get the characters from index 3 up to the end of the string\n\nresult = my_string&#91;-2:]    \/\/Get the last 2 characters of the string\n\nresult = my_string&#91;:-2]    \/\/Get the string excluding the last 2 characters<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert string to value<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#string to int\n#my_variable = int(my_string)\nmy_variable = int(float(my_string))    #This is better as int('0.0') will throw an error in python ('0.0 is not a valid integer of base 10')\n\n#string to float\nmy_variable = float(my_string)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get string from character position # (Remove text from beginning or end of string)<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Getting beginning of a string<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    if my_string.find(\"abc\") &gt;= 0:\n        my_string = my_string&#91;:my_string.index(\"abc\")]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Getting end of string<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    if my_string.find(\"abc\") >= 0:\n        my_string = my_string&#91;my_string.index(\"abc\")+3:]<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Substring Convert string to value Get string from character position # (Remove text from beginning or end of string) Getting beginning of a string Getting end of string<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[319],"tags":[],"class_list":["post-4246","post","type-post","status-publish","format-standard","hentry","category-strings-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4246"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4246\/revisions"}],"predecessor-version":[{"id":4842,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4246\/revisions\/4842"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}