{"id":22318,"date":"2020-09-08T02:05:47","date_gmt":"2020-09-08T09:05:47","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=22318"},"modified":"2023-12-01T03:59:30","modified_gmt":"2023-12-01T11:59:30","slug":"python-remove-first-n-characters-from-string","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/","title":{"rendered":"Remove the First n Characters from a String in Python"},"content":{"rendered":"\n<p>The slicing syntax lets you remove a particular character or range of characters from a string based on the index values of those characters.<br><\/p>\n\n\n\n<p>This guide discusses how to remove the first <code>n<\/code> characters from a string in Python. It walks through an example of the <a href=\"https:\/\/careerkarma.com\/blog\/python-substring\/\">slicing syntax<\/a> so that you can learn how to use it in your own programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python: String Indexing<\/h2>\n\n\n\n<p><a href=\"https:\/\/careerkarma.com\/blog\/python-string-methods\/\">Strings are sequences of characters<\/a>. Each character in a string is given a unique <a href=\"https:\/\/careerkarma.com\/blog\/python-index\/\">index number<\/a>. This number lets you identify and work with a specific character or set of characters.<br><\/p>\n\n\n\n<p>Index numbers begin with zero and increase incrementally by one for each character. Let\u2019s take a look at a string:<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>P<\/td><td>i<\/td><td>e<\/td><td>s<\/td><td>!<\/td><\/tr><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>The string contains four characters. The first character, \u201cP\u201d, has the index number 0. The last character, <code>!<\/code>, has the index number 4.<br><\/p>\n\n\n\n<p>You can use these numbers to retrieve individual characters or remove characters from a string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Remove the First n Characters from a String in Python<\/h2>\n\n\n\n<p>Here, write a program that removes the first four characters from the receipts that are stored by a donut store. These characters represent the ID of a purchase but are no longer relevant due to a systems upgrade.<br><\/p>\n\n\n\n<p>To start, define a <a href=\"https:\/\/careerkarma.com\/blog\/python-array\/\">list of receipts<\/a> and a new list where you can store the new receipts:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>receipts = [\n\t   &quot;107 Strawberry donut $2.00&quot;,\n\t   &quot;297 Blueberry donut $2.10&quot;,\n\t   &quot;342 Raspberry donut $2.10&quot;\n]\nnew_receipts = []<\/pre><\/div>\n\n\n\n<p>The three numbers at the start of the list is the purchase ID. There is also a space that follows the ID that you want to remove.<br><\/p>\n\n\n\n<p>Use a <a href=\"https:\/\/careerkarma.com\/blog\/python-for-loop\/\">\u201cfor\u201d loop<\/a> to iterate over each receipt in the list so you can remove the first four characters from each receipt:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>for r in receipts:\n\t     new_r = r[4:]\n\t     new_receipts.append(new_r)<\/pre><\/div>\n\n\n\n<p>This code removes the first four characters from each receipt in the \u201creceipts\u201d list. A new string without these characters is assigned to the variable \u201cnew_r\u201d with the matched characters removed. You do this because strings are immutable and you cannot modify an existing string.<br><\/p>\n\n\n\n<p>Next, add the new receipt to the \u201cnew_receipts\u201d list. Finally, print the \u201cnew_receipts\u201d list to the console so you can see if the code has worked:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>print(new_receipts)<\/pre><\/div>\n\n\n\n<p>Run the code and see what happens: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['Strawberry donut $2.00', 'Blueberry donut $2.10', 'Raspberry donut $2.10']<\/pre><\/div>\n\n\n\n<p>The code has successfully removed the first four characters from each of the original strings.&nbsp;<br><\/p>\n\n\n\n<p>If you wanted to remove more or fewer characters, you would replace the number 4 with the number of characters you wanted to remove. To remove only the first two characters from the strings, you would use this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>new_r = r[2:]<\/pre><\/div>\n\n\n\n<p>With this line of code, the program would return:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>['7 Strawberry donut $2.00', '7 Blueberry donut $2.10', '2 Raspberry donut $2.10']<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You can remove the first <code>n<\/code> characters from a string using slicing syntax. This syntax lets you retrieve a particular part of a string based on a particular index value.<br><\/p>\n\n\n\n<p>Now you have the knowledge you need to use slicing to remove characters from the start of a Python string like a professional coder!<\/p>\n","protected":false},"excerpt":{"rendered":"The slicing syntax lets you remove a particular character or range of characters from a string based on the index values of those characters. This guide discusses how to remove the first n characters from a string in Python. It walks through an example of the slicing syntax so that you can learn how to&hellip;","protected":false},"author":240,"featured_media":18474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-22318","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Remove the First n Characters from a String in Python | Career Karma<\/title>\n<meta name=\"description\" content=\"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove the First n Characters from a String in Python\" \/>\n<meta property=\"og:description\" content=\"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-08T09:05:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:59:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"574\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Remove the First n Characters from a String in Python\",\"datePublished\":\"2020-09-08T09:05:47+00:00\",\"dateModified\":\"2023-12-01T11:59:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\"},\"wordCount\":478,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\",\"name\":\"Remove the First n Characters from a String in Python | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg\",\"datePublished\":\"2020-09-08T09:05:47+00:00\",\"dateModified\":\"2023-12-01T11:59:30+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg\",\"width\":1020,\"height\":574},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Remove the First n Characters from a String in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Remove the First n Characters from a String in Python | Career Karma","description":"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.","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:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/","og_locale":"en_US","og_type":"article","og_title":"Remove the First n Characters from a String in Python","og_description":"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.","og_url":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-09-08T09:05:47+00:00","article_modified_time":"2023-12-01T11:59:30+00:00","og_image":[{"width":1020,"height":574,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Remove the First n Characters from a String in Python","datePublished":"2020-09-08T09:05:47+00:00","dateModified":"2023-12-01T11:59:30+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/"},"wordCount":478,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/","url":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/","name":"Remove the First n Characters from a String in Python | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg","datePublished":"2020-09-08T09:05:47+00:00","dateModified":"2023-12-01T11:59:30+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"On Career Karma, learn how to use string slicing notation to remove the first \u201cn\u201d characters from a Python string.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/james-harrison-vpOeXr5wmR4-unsplash.jpg","width":1020,"height":574},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-remove-first-n-characters-from-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Remove the First n Characters from a String in Python"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=22318"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/18474"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=22318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=22318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=22318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}