{"id":4732,"date":"2021-04-29T18:19:46","date_gmt":"2021-04-29T12:49:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=4732"},"modified":"2021-11-22T18:53:41","modified_gmt":"2021-11-22T13:23:41","slug":"python-how-to-remove-characters-from-a-string-by-index","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/","title":{"rendered":"Python : How to remove characters from a string by Index ?"},"content":{"rendered":"

Ways to remove i\u2019th character from string in Python<\/h3>\n

Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position.<\/p>\n

So we will discuss different different methods for this.<\/p>\n

Naive Method<\/h3>\n

In this method, we have to first run the loop and append the characters .After that\u00a0 build a new string from the existing one .<\/p>\n

test_str = \"WelcomeBtechGeeks\"\r\n  \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n  \r\n# Removing char at pos 3\r\n# using loop\r\nnew_str = \"\"\r\n  \r\nfor i in range(len(test_str)):\r\n    if i != 2:\r\n        new_str = new_str + test_str[i]\r\n  \r\n# Printing string after removal  \r\nprint (\"The string after removal of i'th character : \" + new_str)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n<\/pre>\n

So in above eoutput you have seen that we have remove character of position three that is ‘l’.<\/p>\n

This method is very slow if we compare with other methods.<\/p>\n

Using str.replace()<\/h3>\n

str.replace() can replace the particular index with empty char, and hence solve the issue.<\/p>\n

test_str = \"WelcomeBtechGeeks\"\r\n  \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n  \r\n# Removing char at pos 3\r\n# using replace\r\nnew_str = test_str.replace('e', '1')\r\n  \r\n# Printing string after removal  \r\n# removes all occurrences of 'e'\r\nprint (\"The string after removal of i'th character( doesn't work) : \" + new_str)\r\n  \r\n# Removing 1st occurrence of e, i.e 2nd pos.\r\n# if we wish to remove it.\r\nnew_str = test_str.replace('e', '', 1)\r\n  \r\n# Printing string after removal  \r\n# removes first occurrences of e\r\nprint (\"The string after removal of i'th character(works) : \" + new_str)\r\n\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character( doesn't work) : W1lcom1Bt1chG11ks\r\nThe string after removal of i'th character(works) : WlcomeBtechGeeks\r\n<\/pre>\n

So in above output you can see that first we have replace all ‘e’ present in original word.After that we replace only first occurrence of e.This method is also not very useful but sometime we are using this.<\/p>\n

Using slice + concatenation<\/h3>\n

In this method we will use string slicing.Then using string concatenation of both, i\u2019th character can appear to be deleted from the string.<\/p>\n

test_str = \"WelcomeBtechGeeks\"\r\n  \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n  \r\n#Removing char at pos 3\r\n# using slice + concatenation\r\nnew_str = test_str[:2] +  test_str[3:]\r\n  \r\n# Printing string after removal  \r\n# removes ele. at 3rd index\r\nprint (\"The string after removal of i'th character : \" + new_str)\r\n\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n\r\n<\/pre>\n

Using\u00a0str.join() and list comprehension<\/h3>\n

In this method each string is converted in list then each of them is joined to make a string.<\/p>\n

test_str = \"WelcomeBtechGeeks\"\r\n  \r\n# Printing original string \r\nprint (\"The original string is : \" + test_str)\r\n  \r\n# Removing char at pos 3\r\n# using join() + list comprehension\r\nnew_str = ''.join([test_str[i] for i in range(len(test_str)) if i != 2])\r\n  \r\n# Printing string after removal  \r\n# removes ele. at 3rd index\r\nprint (\"The string after removal of i'th character : \" + new_str)\r\n\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article3.py\r\nThe original string is : WelcomeBtechGeeks\r\nThe string after removal of i'th character : WecomeBtechGeeks\r\n\r\n<\/pre>\n

Conclusion:<\/h3>\n

So in this article we have seen different method to remove characters from a string in a given range of indices or at specific index position.Enjoy learning guys.<\/p>\n","protected":false},"excerpt":{"rendered":"

Ways to remove i\u2019th character from string in Python Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position. So we will discuss different different methods for this. Naive Method In this method, we have to first run the loop and …<\/p>\n

Python : How to remove characters from a string by Index ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to remove characters from a string by Index ? - Python Programs<\/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:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to remove characters from a string by Index ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Ways to remove i\u2019th character from string in Python Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position. So we will discuss different different methods for this. Naive Method In this method, we have to first run the loop and … Python : How to remove characters from a string by Index ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-29T12:49:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:23:41+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shikha Mishra\" \/>\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\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/\",\"name\":\"Python : How to remove characters from a string by Index ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-04-29T12:49:46+00:00\",\"dateModified\":\"2021-11-22T13:23:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to remove characters from a string by Index ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\"},\"headline\":\"Python : How to remove characters from a string by Index ?\",\"datePublished\":\"2021-04-29T12:49:46+00:00\",\"dateModified\":\"2021-11-22T13:23:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage\"},\"wordCount\":253,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\",\"name\":\"Shikha Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"caption\":\"Shikha Mishra\"},\"url\":\"https:\/\/python-programs.com\/author\/shikha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to remove characters from a string by Index ? - Python Programs","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:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to remove characters from a string by Index ? - Python Programs","og_description":"Ways to remove i\u2019th character from string in Python Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position. So we will discuss different different methods for this. Naive Method In this method, we have to first run the loop and … Python : How to remove characters from a string by Index ? Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-04-29T12:49:46+00:00","article_modified_time":"2021-11-22T13:23:41+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Shikha Mishra","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/","name":"Python : How to remove characters from a string by Index ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-04-29T12:49:46+00:00","dateModified":"2021-11-22T13:23:41+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to remove characters from a string by Index ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594"},"headline":"Python : How to remove characters from a string by Index ?","datePublished":"2021-04-29T12:49:46+00:00","dateModified":"2021-11-22T13:23:41+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#webpage"},"wordCount":253,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-remove-characters-from-a-string-by-index\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594","name":"Shikha Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","caption":"Shikha Mishra"},"url":"https:\/\/python-programs.com\/author\/shikha\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4732"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=4732"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4732\/revisions"}],"predecessor-version":[{"id":4751,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4732\/revisions\/4751"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=4732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=4732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=4732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}