{"id":666,"date":"2023-03-23T16:03:11","date_gmt":"2023-03-23T16:03:11","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=666"},"modified":"2023-03-23T16:03:00","modified_gmt":"2023-03-23T16:03:00","slug":"understanding-slicing-in-python","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/","title":{"rendered":"Understanding slicing in Python"},"content":{"rendered":"\n<p style=\"font-size:15px\"><br>Slicing in Python refers to the technique of selecting a range of elements from a sequence, such as a string, list, or tuple. Slicing is achieved by specifying a range of indices, separated by a colon, within square brackets following the sequence.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Here&#8217;s the syntax for slicing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sequence&#91;start:stop:step]\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" style=\"font-size:16px\">\n<li><code>start<\/code>: The index where the slice starts (inclusive). If <code>start<\/code> is not specified, it defaults to 0.<\/li>\n\n\n\n<li><code>stop<\/code>: The index where the slice ends (exclusive). If <code>stop<\/code> is not specified, it defaults to the end of the sequence.<\/li>\n\n\n\n<li><code>step<\/code>: The step size between elements in the slice. If <code>step<\/code> is not specified, it defaults to 1.<\/li>\n<\/ul>\n\n\n\n<p style=\"font-size:15px\"><br>Let&#8217;s look at some examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> s = \"hello world\"\n>>> s[0:5]   # select the first 5 characters\n'hello'\n\n>>> s[6:]    # select everything after the space\n'world'\n\n>>> s[:5]    # select everything up to the first space\n'hello'\n\n>>> s[::2]   # select every second character\n'hlowrd'\n\n>>> s[::-1]  # reverse the string\n'dlrow olleh'<\/pre>\n\n\n\n<p style=\"font-size:15px\"><br>List of all possible Slice methods format:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>[start:end] &#8211; Returns the slice from &#8216;start&#8217; index to &#8216;end-1&#8217; index.<\/li>\n\n\n\n<li>[start:] &#8211; Returns the slice from &#8216;start&#8217; index to the end of the sequence.<\/li>\n\n\n\n<li>[:end] &#8211; Returns the slice from the beginning of the sequence to &#8216;end-1&#8217; index.<\/li>\n\n\n\n<li>[start:end:step] &#8211; Returns a slice from &#8216;start&#8217; to &#8216;end-1&#8217; index with &#8216;step&#8217; intervals.<\/li>\n\n\n\n<li>[::-1] &#8211; Returns the reverse of the sequence.<\/li>\n\n\n\n<li>[:] &#8211; Returns a copy of the entire sequence.<\/li>\n\n\n\n<li>[::] &#8211; Returns a copy of the entire sequence.<\/li>\n\n\n\n<li>[start:end:step] &#8211; Returns a slice from &#8216;start&#8217; to &#8216;end-1&#8217; index with &#8216;step&#8217; intervals.<\/li>\n\n\n\n<li>[-1] &#8211; Returns the last item of the sequence.<\/li>\n\n\n\n<li>[-n:] &#8211; Returns the last &#8216;n&#8217; items of the sequence.<\/li>\n\n\n\n<li>[:-n] &#8211; Returns all the items of the sequence except the last &#8216;n&#8217; items.<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:15px\"><br>All possible sample code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_list = [1, 2, 3, 4, 5]\n\n# Get the first element\nprint(my_list[0])\n\n# Get the last element\nprint(my_list[-1])\n\n# Get the first three elements\nprint(my_list[:3])\n\n# Get the last two elements\nprint(my_list[-2:])\n\n# Get every other element starting from the second\nprint(my_list[1::2])\n\n# Reverse the list\nprint(my_list[::-1])\n\n# Get the second through fourth elements\nprint(my_list[1:4])\n\n# Get every other element starting from the first\nprint(my_list[::2])\n\n# Get every third element starting from the second to the second last element\nprint(my_list[1:-1:3])\n\n# Assign new values to elements using slice notation\nmy_list[:3] = [0, 0, 0]\nprint(my_list)\n\n# Delete elements using slice notation\ndel my_list[-2:]\nprint(my_list)\n\n# Copy the entire list using slice notation\ncopy_list = my_list[:]\nprint(copy_list)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Slicing in Python refers to the technique of selecting a range of elements from a sequence, such as a string, list, or tuple. Slicing is achieved by specifying a range of indices, separated by a colon, within square brackets following the sequence. Here&#8217;s the syntax for slicing: Let&#8217;s look at some examples: List of all [&hellip;]<\/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,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[42,12],"tags":[14,13,16,80,15,86],"class_list":["post-666","post","type-post","status-publish","format-standard","hentry","category-general-qa","category-python","tag-programming-language","tag-python","tag-python-code","tag-python-qna","tag-python-sample-code","tag-slicing"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding slicing in Python - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding slicing in Python - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Slicing in Python refers to the technique of selecting a range of elements from a sequence, such as a string, list, or tuple. Slicing is achieved by specifying a range of indices, separated by a colon, within square brackets following the sequence. Here&#8217;s the syntax for slicing: Let&#8217;s look at some examples: List of all [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-23T16:03:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-23T16:03:00+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/\",\"name\":\"Understanding slicing in Python - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-03-23T16:03:11+00:00\",\"dateModified\":\"2023-03-23T16:03:00+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding slicing in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding slicing in Python - tinyytopic.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Understanding slicing in Python - tinyytopic.com","og_description":"Slicing in Python refers to the technique of selecting a range of elements from a sequence, such as a string, list, or tuple. Slicing is achieved by specifying a range of indices, separated by a colon, within square brackets following the sequence. Here&#8217;s the syntax for slicing: Let&#8217;s look at some examples: List of all [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/","og_site_name":"tinyytopic.com","article_published_time":"2023-03-23T16:03:11+00:00","article_modified_time":"2023-03-23T16:03:00+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/","name":"Understanding slicing in Python - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-03-23T16:03:11+00:00","dateModified":"2023-03-23T16:03:00+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/23\/understanding-slicing-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"Understanding slicing in Python"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":6,"uagb_excerpt":"Slicing in Python refers to the technique of selecting a range of elements from a sequence, such as a string, list, or tuple. Slicing is achieved by specifying a range of indices, separated by a colon, within square brackets following the sequence. Here&#8217;s the syntax for slicing: Let&#8217;s look at some examples: List of all&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":3,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/666\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}